summaryrefslogtreecommitdiff
path: root/gxp-firmware-data.c
blob: 6f22f8dd6837721aa77108a7f827fca69793114e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
// SPDX-License-Identifier: GPL-2.0
/*
 * GXP firmware data manager.
 *
 * Copyright (C) 2021 Google LLC
 */

#include <linux/bitops.h>
#include <linux/dma-mapping.h>
#include <linux/genalloc.h>

#include "gxp-debug-dump.h"
#include "gxp-firmware-data.h"
#include "gxp-host-device-structs.h"
#include "gxp-internal.h"
#include "gxp-range-alloc.h"
#include "gxp.h"

/*
 * The minimum alignment order (power of 2) of allocations in the firmware data
 * region.
 */
#define FW_DATA_STORAGE_ORDER		3

/* A byte pattern to pre-populate the FW region with */
#define FW_DATA_DEBUG_PATTERN		0x66

/* IDs for dedicated doorbells used by some system components */
#define DOORBELL_ID_CORE_WAKEUP(__core__) (0 + __core__)

/* IDs for dedicated sync barriers used by some system components */
#define SYNC_BARRIER_ID_UART		1

/* Default application parameters */
#define DEFAULT_APP_ID			1
#define DEFAULT_APP_USER_MEM_SIZE	(120 * 1024)
#define DEFAULT_APP_USER_MEM_ALIGNMENT	8
#define DEFAULT_APP_THREAD_COUNT	2
#define DEFAULT_APP_TCM_PER_BANK	(100 * 1024)
#define DEFAULT_APP_USER_DOORBELL_COUNT	2
#define DEFAULT_APP_USER_BARRIER_COUNT	2

/* Core-to-core mailbox communication constants */
#define CORE_TO_CORE_MBX_CMD_COUNT	10
#define CORE_TO_CORE_MBX_RSP_COUNT	10

/* A block allocator managing and partitioning a memory region for device use */
struct fw_memory_allocator {
	struct gen_pool *pool;
	struct gxp_dev *gxp;
	void *base_host_addr;
	uint32_t base_device_addr;
};

/* A memory region allocated for device use */
struct fw_memory {
	void *host_addr;
	uint32_t device_addr;
	size_t sz;
};

/*
 * Holds information about system-wide HW and memory resources given to the FWs
 * of GXP devices.
 */
struct gxp_fw_data_manager {
	/* Host-side pointers for book keeping */
	void *fw_data_virt;
	struct gxp_system_descriptor *system_desc;

	/* Doorbells allocator and reserved doorbell IDs */
	struct range_alloc *doorbell_allocator;
	int core_wakeup_doorbells[GXP_NUM_WAKEUP_DOORBELLS];
	int semaphore_doorbells[GXP_NUM_CORES];

	/* Sync barriers allocator and reserved sync barrier IDs */
	struct range_alloc *sync_barrier_allocator;
	int uart_sync_barrier;
	int timer_regions_barrier;
	int watchdog_region_barrier;
	int uart_region_barrier;
	int doorbell_regions_barrier;
	int sync_barrier_regions_barrier;
	int semaphores_regions_barrier;

	/* System-wide device memory resources */
	struct fw_memory_allocator *allocator;
	struct fw_memory sys_desc_mem;
	struct fw_memory wdog_mem;
	struct fw_memory core_telemetry_mem;
	struct fw_memory debug_dump_mem;
};

/* A container holding information for a single GXP application. */
struct app_metadata {
	struct gxp_fw_data_manager *mgr;
	uint application_id;
	uint core_count;
	uint core_list; /* bitmap of cores allocated to this app */

	/* Per-app doorbell IDs */
	int user_doorbells_count;
	int *user_doorbells;

	/* Per-app sync barrier IDs */
	int user_barriers_count;
	int *user_barriers;

	/* Per-app memory regions */
	struct fw_memory user_mem;
	struct fw_memory doorbells_mem;
	struct fw_memory sync_barriers_mem;
	struct fw_memory semaphores_mem;
	struct fw_memory cores_mem;
	struct fw_memory core_cmd_queues_mem[GXP_NUM_CORES];
	struct fw_memory core_rsp_queues_mem[GXP_NUM_CORES];
	struct fw_memory app_mem;
};

static struct fw_memory_allocator *mem_alloc_create(struct gxp_dev *gxp,
						    void *host_base,
						    uint32_t device_base,
						    size_t size)
{
	struct fw_memory_allocator *allocator;
	int ret = 0;

	allocator = kzalloc(sizeof(*allocator), GFP_KERNEL);
	if (!allocator)
		return ERR_PTR(-ENOMEM);

	/*
	 * Use a genpool to allocate and free chunks of the virtual address
	 * space reserved for FW data. The genpool doesn't use the passed
	 * addresses internally to access any data, thus it is safe to use it to
	 * manage memory that the host may not be able to access directly.
	 * The allocator also records the host-side address so that the code
	 * here can access and populate data in this region.
	 */
	allocator->gxp = gxp;
	allocator->pool = gen_pool_create(FW_DATA_STORAGE_ORDER, /*nid=*/-1);
	if (!allocator->pool) {
		dev_err(gxp->dev, "Failed to create memory pool\n");
		kfree(allocator);
		return ERR_PTR(-ENOMEM);
	}

	ret = gen_pool_add(allocator->pool, device_base, size, /*nid=*/-1);
	if (ret) {
		dev_err(gxp->dev, "Failed to add memory to pool (ret = %d)\n",
			ret);
		gen_pool_destroy(allocator->pool);
		kfree(allocator);
		return ERR_PTR(ret);
	}
	allocator->base_host_addr = host_base;
	allocator->base_device_addr = device_base;

	return allocator;
}

static int mem_alloc_allocate(struct fw_memory_allocator *allocator,
			      struct fw_memory *mem, size_t size,
			      uint8_t alignment)
{
	struct genpool_data_align data = { .align = alignment };
	uint32_t dev_addr;

	dev_addr = gen_pool_alloc_algo(allocator->pool, size,
				       gen_pool_first_fit_align, &data);
	if (!dev_addr)
		return -ENOMEM;

	mem->host_addr = allocator->base_host_addr +
			 (dev_addr - allocator->base_device_addr);
	mem->device_addr = dev_addr;
	mem->sz = size;

	return 0;
}

static void mem_alloc_free(struct fw_memory_allocator *allocator,
			   struct fw_memory *mem)
{
	gen_pool_free(allocator->pool, mem->device_addr, mem->sz);
}

static void mem_alloc_destroy(struct fw_memory_allocator *allocator)
{
	WARN_ON(gen_pool_avail(allocator->pool) !=
		gen_pool_size(allocator->pool));
	gen_pool_destroy(allocator->pool);
	kfree(allocator);
}

static struct fw_memory init_doorbells(struct app_metadata *app)
{
	struct gxp_doorbells_descriptor *db_region;
	struct fw_memory mem;
	uint32_t mem_size;
	uint32_t doorbell_count;
	int i;

	doorbell_count = app->user_doorbells_count;
	mem_size = sizeof(*db_region) +
		   doorbell_count * sizeof(db_region->doorbells[0]);

	mem_alloc_allocate(app->mgr->allocator, &mem, mem_size,
			   __alignof__(struct gxp_doorbells_descriptor));

	db_region = mem.host_addr;
	db_region->application_id = app->application_id;
	db_region->protection_barrier = app->mgr->doorbell_regions_barrier;
	db_region->num_items = doorbell_count;
	for (i = 0; i < doorbell_count; i++) {
		db_region->doorbells[i].users_count = 0;
		db_region->doorbells[i].hw_doorbell_idx =
			app->user_doorbells[i];
	}

	return mem;
}

static struct fw_memory init_sync_barriers(struct app_metadata *app)
{
	struct gxp_sync_barriers_descriptor *sb_region;
	struct fw_memory mem;
	uint32_t mem_size;
	uint32_t barrier_count;
	int i;

	barrier_count = app->user_barriers_count;
	mem_size = sizeof(*sb_region) +
		   barrier_count * sizeof(sb_region->barriers[0]);

	mem_alloc_allocate(app->mgr->allocator, &mem, mem_size,
			   __alignof__(struct gxp_sync_barriers_descriptor));

	sb_region = mem.host_addr;
	sb_region->application_id = app->application_id;
	sb_region->protection_barrier = app->mgr->sync_barrier_regions_barrier;
	sb_region->num_items = barrier_count;
	for (i = 0; i < barrier_count; i++) {
		sb_region->barriers[i].users_count = 0;
		sb_region->barriers[i].hw_barrier_idx = app->user_barriers[i];
	}

	return mem;
}

static struct fw_memory init_watchdog(struct gxp_fw_data_manager *mgr)
{
	struct gxp_watchdog_descriptor *wd_region;
	struct fw_memory mem;

	mem_alloc_allocate(mgr->allocator, &mem, sizeof(*wd_region),
			   __alignof__(struct gxp_watchdog_descriptor));

	wd_region = mem.host_addr;
	wd_region->protection_barrier = mgr->watchdog_region_barrier;
	wd_region->target_value = 0;
	wd_region->participating_cores = 0;
	wd_region->responded_cores = 0;
	wd_region->tripped = 0;

	return mem;
}

static struct fw_memory init_core_telemetry(struct gxp_fw_data_manager *mgr)
{
	struct gxp_core_telemetry_descriptor *tel_region;
	struct fw_memory mem;

	mem_alloc_allocate(mgr->allocator, &mem, sizeof(*tel_region),
			   __alignof__(struct gxp_core_telemetry_descriptor));

	tel_region = mem.host_addr;

	/*
	 * Core telemetry is disabled for now.
	 * Subsuequent calls to the FW data module can be used to populate or
	 * depopulate the descriptor pointers on demand.
	 */
	memset(tel_region, 0x00, sizeof(*tel_region));

	return mem;
}

static struct fw_memory init_debug_dump(struct gxp_dev *gxp)
{
	struct fw_memory mem;

	if (gxp->debug_dump_mgr) {
		mem.host_addr = gxp->debug_dump_mgr->buf.vaddr;
		mem.device_addr = gxp->debug_dump_mgr->buf.dsp_addr;
		mem.sz = gxp->debug_dump_mgr->buf.size;
	} else {
		mem.host_addr = 0;
		mem.device_addr = 0;
		mem.sz = 0;
	}

	return mem;
}

static struct fw_memory init_app_user_memory(struct app_metadata *app,
					     int memory_size)
{
	struct fw_memory mem;

	mem_alloc_allocate(app->mgr->allocator, &mem, memory_size,
			   DEFAULT_APP_USER_MEM_ALIGNMENT);

	return mem;
}

static struct fw_memory init_app_semaphores(struct app_metadata *app)
{
	struct gxp_semaphores_descriptor *sm_region;
	struct fw_memory mem;
	uint32_t mem_size;
	uint32_t semaphore_count;
	int core;
	int i;

	semaphore_count = NUM_SYSTEM_SEMAPHORES;
	mem_size = sizeof(*sm_region) +
		   semaphore_count * sizeof(sm_region->semaphores[0]);

	mem_alloc_allocate(app->mgr->allocator, &mem, mem_size,
			   __alignof__(struct gxp_semaphores_descriptor));

	sm_region = mem.host_addr;
	sm_region->application_id = app->application_id;
	sm_region->protection_barrier = app->mgr->semaphores_regions_barrier;

	core = 0;
	for (i = 0; i < GXP_NUM_CORES; i++) {
		if (app->core_list & BIT(i))
			sm_region->wakeup_doorbells[core++] =
				app->mgr->semaphore_doorbells[i];
		sm_region->woken_pending_semaphores[i] = 0;
	}

	sm_region->num_items = semaphore_count;
	for (i = 0; i < semaphore_count; i++) {
		sm_region->semaphores[i].users_count = 0;
		sm_region->semaphores[i].count = 0;
		sm_region->semaphores[i].waiters = 0;
	}

	return mem;
}

static struct fw_memory init_app_cores(struct app_metadata *app)
{
	struct gxp_cores_descriptor *cd_region;
	struct gxp_queue_info *q_info;
	struct fw_memory mem;
	uint32_t mem_size;
	int semaphore_id;
	int core_count;
	int i;
	const int cmd_queue_items = CORE_TO_CORE_MBX_CMD_COUNT;
	const int resp_queue_items = CORE_TO_CORE_MBX_RSP_COUNT;

	/* Core info structures. */
	core_count = app->core_count;
	mem_size =
		sizeof(*cd_region) + core_count * sizeof(cd_region->cores[0]);

	mem_alloc_allocate(app->mgr->allocator, &mem, mem_size,
			   __alignof__(struct gxp_cores_descriptor));

	cd_region = mem.host_addr;
	cd_region->num_items = core_count;

	/* Command and response queues. */
	semaphore_id = 0;
	for (i = 0; i < core_count; i++) {
		/* Allocate per-core command queue storage. */
		mem_size = cmd_queue_items *
			   sizeof(struct gxp_core_to_core_command);
		mem_alloc_allocate(
			app->mgr->allocator, &app->core_cmd_queues_mem[i],
			mem_size, __alignof__(struct gxp_core_to_core_command));

		/* Update per-core command queue info. */
		q_info = &cd_region->cores[i].incoming_commands_queue;
		q_info->header.storage =
			app->core_cmd_queues_mem[i].device_addr;
		q_info->header.head_idx = 0;
		q_info->header.tail_idx = 0;
		q_info->header.element_size =
			sizeof(struct gxp_core_to_core_command);
		q_info->header.elements_count = cmd_queue_items;
		q_info->access_sem_id = semaphore_id++;
		q_info->posted_slots_sem_id = semaphore_id++;
		q_info->free_slots_sem_id = semaphore_id++;

		/* Allocate per-core response queue storage. */
		mem_size = resp_queue_items *
			   sizeof(struct gxp_core_to_core_response);
		mem_alloc_allocate(
			app->mgr->allocator, &app->core_rsp_queues_mem[i],
			mem_size,
			__alignof__(struct gxp_core_to_core_response));

		/* Update per-core response queue info. */
		q_info = &cd_region->cores[i].incoming_responses_queue;
		q_info->header.storage =
			app->core_rsp_queues_mem[i].device_addr;
		q_info->header.head_idx = 0;
		q_info->header.tail_idx = 0;
		q_info->header.element_size =
			sizeof(struct gxp_core_to_core_response);
		q_info->header.elements_count = resp_queue_items;
		q_info->access_sem_id = semaphore_id++;
		q_info->posted_slots_sem_id = semaphore_id++;
		q_info->free_slots_sem_id = semaphore_id++;
	}

	return mem;
}

static struct fw_memory init_application(struct app_metadata *app)
{
	struct gxp_application_descriptor *app_region;
	struct fw_memory mem;
	const int user_mem_size = DEFAULT_APP_USER_MEM_SIZE;

	/* App's system memory. */
	app->user_mem = init_app_user_memory(app, user_mem_size);

	/* App's doorbells region. */
	app->doorbells_mem = init_doorbells(app);

	/* App's  sync barriers region. */
	app->sync_barriers_mem = init_sync_barriers(app);

	/* App's semaphores region. */
	app->semaphores_mem = init_app_semaphores(app);

	/* App's cores info and core-to-core queues. */
	app->cores_mem = init_app_cores(app);

	/* App's descriptor. */
	mem_alloc_allocate(app->mgr->allocator, &mem, sizeof(*app_region),
			   __alignof__(struct gxp_application_descriptor));
	app_region = mem.host_addr;
	app_region->application_id = app->application_id;
	app_region->core_count = app->core_count;
	app_region->cores_mask = app->core_list;
	app_region->threads_count = DEFAULT_APP_THREAD_COUNT;
	app_region->tcm_memory_per_bank = DEFAULT_APP_TCM_PER_BANK;
	app_region->system_memory_size = user_mem_size;
	app_region->system_memory_addr = app->user_mem.device_addr;
	app_region->doorbells_dev_addr = app->doorbells_mem.device_addr;
	app_region->sync_barriers_dev_addr = app->sync_barriers_mem.device_addr;
	app_region->semaphores_dev_addr = app->semaphores_mem.device_addr;
	app_region->cores_info_dev_addr = app->cores_mem.device_addr;

	return mem;
}

int gxp_fw_data_init(struct gxp_dev *gxp)
{
	struct gxp_fw_data_manager *mgr;
	int res;
	int i;

	mgr = devm_kzalloc(gxp->dev, sizeof(*mgr), GFP_KERNEL);
	if (!mgr)
		return -ENOMEM;
	gxp->data_mgr = mgr;

	/*
	 * TODO (b/200169232) Using memremap until devm_memremap is added to
	 * the GKI ABI
	 */
	mgr->fw_data_virt = memremap(gxp->fwdatabuf.paddr, gxp->fwdatabuf.size,
				     MEMREMAP_WC);

	if (IS_ERR_OR_NULL(mgr->fw_data_virt)) {
		dev_err(gxp->dev, "Failed to map fw data region\n");
		res = -ENODEV;
		goto err;
	}

	/* Instantiate the doorbells allocator with all doorbells */
	mgr->doorbell_allocator =
		range_alloc_create(/*start=*/0, DOORBELL_COUNT);
	if (IS_ERR(mgr->doorbell_allocator)) {
		dev_err(gxp->dev, "Failed to create doorbells allocator\n");
		res = PTR_ERR(mgr->doorbell_allocator);
		mgr->doorbell_allocator = NULL;
		goto err;
	}

	/* Instantiate the sync barriers allocator with all sync barriers */
	mgr->sync_barrier_allocator =
		range_alloc_create(/*start=*/0, SYNC_BARRIER_COUNT);
	if (IS_ERR(mgr->sync_barrier_allocator)) {
		dev_err(gxp->dev, "Failed to create sync barriers allocator\n");
		res = PTR_ERR(mgr->sync_barrier_allocator);
		mgr->sync_barrier_allocator = NULL;
		goto err;
	}

	/* Allocate doorbells */

	/* Pinned: Cores wakeup doorbell */
	for (i = 0; i < GXP_NUM_WAKEUP_DOORBELLS; i++) {
		mgr->core_wakeup_doorbells[i] = DOORBELL_ID_CORE_WAKEUP(i);
		res = range_alloc_get(mgr->doorbell_allocator,
				      mgr->core_wakeup_doorbells[i]);
		if (res)
			goto err;
	}

	/* Semaphores operation doorbells */
	for (i = 0; i < GXP_NUM_CORES; i++) {
		range_alloc_get_any(mgr->doorbell_allocator,
				    &mgr->semaphore_doorbells[i]);
	}

	/* Allocate sync barriers */

	/* Pinned: UART sync barrier */
	mgr->uart_sync_barrier = SYNC_BARRIER_ID_UART;
	mgr->uart_region_barrier = SYNC_BARRIER_ID_UART;
	res = range_alloc_get(mgr->sync_barrier_allocator,
			      mgr->uart_sync_barrier);
	if (res)
		goto err;

	/* Doorbell regions for all apps */
	res = range_alloc_get_any(mgr->sync_barrier_allocator,
				  &mgr->doorbell_regions_barrier);
	if (res)
		goto err;

	/* Sync barrier regions for all apps */
	res = range_alloc_get_any(mgr->sync_barrier_allocator,
				  &mgr->sync_barrier_regions_barrier);
	if (res)
		goto err;

	/* Timer regions for all apps */
	res = range_alloc_get_any(mgr->sync_barrier_allocator,
				  &mgr->timer_regions_barrier);
	if (res)
		goto err;

	/* Watchdog regions for all apps */
	res = range_alloc_get_any(mgr->sync_barrier_allocator,
				  &mgr->watchdog_region_barrier);
	if (res)
		goto err;

	/* Semaphore regions for all apps */
	res = range_alloc_get_any(mgr->sync_barrier_allocator,
				  &mgr->semaphores_regions_barrier);
	if (res)
		goto err;

	/* Shared firmware data memory region */
	mgr->allocator = mem_alloc_create(gxp, mgr->fw_data_virt,
					  gxp->fwdatabuf.daddr,
					  gxp->fwdatabuf.size);
	if (IS_ERR(mgr->allocator)) {
		dev_err(gxp->dev,
			"Failed to create the FW data memory allocator\n");
		res = PTR_ERR(mgr->allocator);
		mgr->allocator = NULL;
		goto err;
	}

	/* Populate the region with a pre-defined pattern. */
	memset(mgr->fw_data_virt, FW_DATA_DEBUG_PATTERN, gxp->fwdatabuf.size);

	/* Allocate the root system descriptor from the region */
	mem_alloc_allocate(mgr->allocator, &mgr->sys_desc_mem,
			   sizeof(struct gxp_system_descriptor),
			   __alignof__(struct gxp_system_descriptor));
	mgr->system_desc = mgr->sys_desc_mem.host_addr;

	/* Allocate the watchdog descriptor from the region */
	mgr->wdog_mem = init_watchdog(mgr);
	mgr->system_desc->watchdog_dev_addr = mgr->wdog_mem.device_addr;

	/* Allocate the descriptor for device-side core telemetry */
	mgr->core_telemetry_mem = init_core_telemetry(mgr);
	mgr->system_desc->core_telemetry_dev_addr =
		mgr->core_telemetry_mem.device_addr;

	/* Set the debug dump region parameters if available */
	mgr->debug_dump_mem = init_debug_dump(gxp);
	mgr->system_desc->debug_dump_dev_addr = mgr->debug_dump_mem.device_addr;

	return res;

err:
	range_alloc_destroy(mgr->sync_barrier_allocator);
	range_alloc_destroy(mgr->doorbell_allocator);
	devm_kfree(gxp->dev, mgr);
	return res;
}

void *gxp_fw_data_create_app(struct gxp_dev *gxp, uint core_list)
{
	struct gxp_fw_data_manager *mgr = gxp->data_mgr;
	struct app_metadata *app;
	void *err;
	int i;

	app = kzalloc(sizeof(struct app_metadata), GFP_KERNEL);
	if (!app)
		return ERR_PTR(-ENOMEM);

	/* Create resource and memory allocations for new app */
	app->mgr = mgr;
	app->application_id = DEFAULT_APP_ID;
	app->core_count = hweight_long(core_list);
	app->core_list = core_list;

	/* User doorbells */
	app->user_doorbells_count = DEFAULT_APP_USER_DOORBELL_COUNT;
	app->user_doorbells =
		kcalloc(app->user_doorbells_count, sizeof(int), GFP_KERNEL);
	if (!app->user_doorbells) {
		err = ERR_PTR(-ENOMEM);
		goto err_user_doorbells;
	}

	for (i = 0; i < app->user_doorbells_count; i++) {
		range_alloc_get_any(mgr->doorbell_allocator,
				    &app->user_doorbells[i]);
	}

	/* User sync barrier */
	app->user_barriers_count = DEFAULT_APP_USER_BARRIER_COUNT;
	app->user_barriers =
		kcalloc(app->user_barriers_count, sizeof(int), GFP_KERNEL);
	if (!app->user_barriers) {
		err = ERR_PTR(-ENOMEM);
		goto err_user_barriers;
	}

	for (i = 0; i < app->user_barriers_count; i++) {
		range_alloc_get_any(mgr->sync_barrier_allocator,
				    &app->user_barriers[i]);
	}

	/* Application region. */
	app->app_mem = init_application(app);
	for (i = 0; i < GXP_NUM_CORES; i++) {
		if (core_list & BIT(i)) {
			mgr->system_desc->app_descriptor_dev_addr[i] =
				app->app_mem.device_addr;
		}
	}

	return app;

err_user_barriers:
	for (i = 0; i < app->user_doorbells_count; i++)
		range_alloc_put(mgr->doorbell_allocator,
				app->user_doorbells[i]);
	kfree(app->user_doorbells);
err_user_doorbells:
	kfree(app);

	return err;
}

void gxp_fw_data_destroy_app(struct gxp_dev *gxp, void *application)
{
	struct app_metadata *app = application;
	struct gxp_fw_data_manager *mgr = gxp->data_mgr;
	int i;

	for (i = 0; i < app->user_doorbells_count; i++)
		range_alloc_put(mgr->doorbell_allocator,
				app->user_doorbells[i]);
	kfree(app->user_doorbells);

	for (i = 0; i < app->user_barriers_count; i++)
		range_alloc_put(mgr->sync_barrier_allocator,
				app->user_barriers[i]);
	kfree(app->user_barriers);

	mem_alloc_free(mgr->allocator, &app->user_mem);
	mem_alloc_free(mgr->allocator, &app->doorbells_mem);
	mem_alloc_free(mgr->allocator, &app->sync_barriers_mem);
	mem_alloc_free(mgr->allocator, &app->semaphores_mem);
	mem_alloc_free(mgr->allocator, &app->cores_mem);
	for (i = 0; i < app->core_count; i++) {
		mem_alloc_free(mgr->allocator, &app->core_cmd_queues_mem[i]);
		mem_alloc_free(mgr->allocator, &app->core_rsp_queues_mem[i]);
	}
	mem_alloc_free(mgr->allocator, &app->app_mem);

	kfree(app);
}

void gxp_fw_data_destroy(struct gxp_dev *gxp)
{
	struct gxp_fw_data_manager *mgr = gxp->data_mgr;

	if (!mgr)
		return;

	mem_alloc_free(mgr->allocator, &mgr->core_telemetry_mem);
	mem_alloc_free(mgr->allocator, &mgr->wdog_mem);
	mem_alloc_free(mgr->allocator, &mgr->sys_desc_mem);
	mem_alloc_destroy(mgr->allocator);

	range_alloc_destroy(mgr->sync_barrier_allocator);
	range_alloc_destroy(mgr->doorbell_allocator);

	/* TODO (b/200169232) Remove this once we're using devm_memremap */
	if (mgr->fw_data_virt) {
		memunmap(mgr->fw_data_virt);
		mgr->fw_data_virt = NULL;
	}

	if (gxp->data_mgr) {
		devm_kfree(gxp->dev, gxp->data_mgr);
		gxp->data_mgr = NULL;
	}
}

int gxp_fw_data_set_core_telemetry_descriptors(struct gxp_dev *gxp, u8 type,
					       u32 host_status,
					       struct gxp_coherent_buf *buffers,
					       u32 per_buffer_size)
{
	struct gxp_core_telemetry_descriptor *descriptor =
		gxp->data_mgr->core_telemetry_mem.host_addr;
	struct core_telemetry_descriptor *core_descriptors;
	uint core;
	bool enable;

	if (type == GXP_TELEMETRY_TYPE_LOGGING)
		core_descriptors = descriptor->per_core_loggers;
	else if (type == GXP_TELEMETRY_TYPE_TRACING)
		core_descriptors = descriptor->per_core_tracers;
	else
		return -EINVAL;

	enable = (host_status & GXP_CORE_TELEMETRY_HOST_STATUS_ENABLED);

	if (enable) {
		/* Validate that the provided IOVAs are addressable (i.e. 32-bit) */
		for (core = 0; core < GXP_NUM_CORES; core++) {
			if (buffers && buffers[core].dsp_addr > U32_MAX &&
				buffers[core].size == per_buffer_size)
				return -EINVAL;
		}

		for (core = 0; core < GXP_NUM_CORES; core++) {
			core_descriptors[core].host_status = host_status;
			core_descriptors[core].buffer_addr = (u32)buffers[core].dsp_addr;
			core_descriptors[core].buffer_size = per_buffer_size;
		}
	} else {
		for (core = 0; core < GXP_NUM_CORES; core++) {
			core_descriptors[core].host_status = host_status;
			core_descriptors[core].buffer_addr = 0;
			core_descriptors[core].buffer_size = 0;
		}
	}

	return 0;
}

u32 gxp_fw_data_get_core_telemetry_device_status(struct gxp_dev *gxp, uint core,
						 u8 type)
{
	struct gxp_core_telemetry_descriptor *descriptor =
		gxp->data_mgr->core_telemetry_mem.host_addr;

	if (core >= GXP_NUM_CORES)
		return 0;

	switch (type) {
	case GXP_TELEMETRY_TYPE_LOGGING:
		return descriptor->per_core_loggers[core].device_status;
	case GXP_TELEMETRY_TYPE_TRACING:
		return descriptor->per_core_tracers[core].device_status;
	default:
		return 0;
	}
}