smem.c 20.5 KB
Newer Older
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
/*
 * Copyright (c) 2015, Sony Mobile Communications AB.
 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/hwspinlock.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/soc/qcom/smem.h>

/*
 * The Qualcomm shared memory system is a allocate only heap structure that
 * consists of one of more memory areas that can be accessed by the processors
 * in the SoC.
 *
 * All systems contains a global heap, accessible by all processors in the SoC,
 * with a table of contents data structure (@smem_header) at the beginning of
 * the main shared memory block.
 *
 * The global header contains meta data for allocations as well as a fixed list
 * of 512 entries (@smem_global_entry) that can be initialized to reference
 * parts of the shared memory space.
 *
 *
 * In addition to this global heap a set of "private" heaps can be set up at
 * boot time with access restrictions so that only certain processor pairs can
 * access the data.
 *
 * These partitions are referenced from an optional partition table
 * (@smem_ptable), that is found 4kB from the end of the main smem region. The
 * partition table entries (@smem_ptable_entry) lists the involved processors
 * (or hosts) and their location in the main shared memory region.
 *
 * Each partition starts with a header (@smem_partition_header) that identifies
 * the partition and holds properties for the two internal memory regions. The
 * two regions are cached and non-cached memory respectively. Each region
 * contain a link list of allocation headers (@smem_private_entry) followed by
 * their data.
 *
 * Items in the non-cached region are allocated from the start of the partition
 * while items in the cached region are allocated from the end. The free area
 * is hence the region between the cached and non-cached offsets.
 *
 *
 * To synchronize allocations in the shared memory heaps a remote spinlock must
 * be held - currently lock number 3 of the sfpb or tcsr is used for this on all
 * platforms.
 *
 */

/*
 * Item 3 of the global heap contains an array of versions for the various
 * software components in the SoC. We verify that the boot loader version is
 * what the expected version (SMEM_EXPECTED_VERSION) as a sanity check.
 */
#define SMEM_ITEM_VERSION	3
#define  SMEM_MASTER_SBL_VERSION_INDEX	7
#define  SMEM_EXPECTED_VERSION		11

/*
 * The first 8 items are only to be allocated by the boot loader while
 * initializing the heap.
 */
#define SMEM_ITEM_LAST_FIXED	8

/* Highest accepted item number, for both global and private heaps */
#define SMEM_ITEM_COUNT		512

/* Processor/host identifier for the application processor */
#define SMEM_HOST_APPS		0

/* Max number of processors/hosts in a system */
#define SMEM_HOST_COUNT		9

/**
  * struct smem_proc_comm - proc_comm communication struct (legacy)
  * @command:	current command to be executed
  * @status:	status of the currently requested command
  * @params:	parameters to the command
  */
struct smem_proc_comm {
95 96 97
	__le32 command;
	__le32 status;
	__le32 params[2];
98 99 100 101 102 103 104 105 106 107 108
};

/**
 * struct smem_global_entry - entry to reference smem items on the heap
 * @allocated:	boolean to indicate if this entry is used
 * @offset:	offset to the allocated space
 * @size:	size of the allocated space, 8 byte aligned
 * @aux_base:	base address for the memory region used by this unit, or 0 for
 *		the default region. bits 0,1 are reserved
 */
struct smem_global_entry {
109 110 111 112
	__le32 allocated;
	__le32 offset;
	__le32 size;
	__le32 aux_base; /* bits 1:0 reserved */
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
};
#define AUX_BASE_MASK		0xfffffffc

/**
 * struct smem_header - header found in beginning of primary smem region
 * @proc_comm:		proc_comm communication interface (legacy)
 * @version:		array of versions for the various subsystems
 * @initialized:	boolean to indicate that smem is initialized
 * @free_offset:	index of the first unallocated byte in smem
 * @available:		number of bytes available for allocation
 * @reserved:		reserved field, must be 0
 * toc:			array of references to items
 */
struct smem_header {
	struct smem_proc_comm proc_comm[4];
128 129 130 131 132
	__le32 version[32];
	__le32 initialized;
	__le32 free_offset;
	__le32 available;
	__le32 reserved;
133 134 135 136 137 138 139 140 141 142 143 144 145
	struct smem_global_entry toc[SMEM_ITEM_COUNT];
};

/**
 * struct smem_ptable_entry - one entry in the @smem_ptable list
 * @offset:	offset, within the main shared memory region, of the partition
 * @size:	size of the partition
 * @flags:	flags for the partition (currently unused)
 * @host0:	first processor/host with access to this partition
 * @host1:	second processor/host with access to this partition
 * @reserved:	reserved entries for later use
 */
struct smem_ptable_entry {
146 147 148 149 150 151
	__le32 offset;
	__le32 size;
	__le32 flags;
	__le16 host0;
	__le16 host1;
	__le32 reserved[8];
152 153 154 155 156 157 158 159 160 161 162
};

/**
 * struct smem_ptable - partition table for the private partitions
 * @magic:	magic number, must be SMEM_PTABLE_MAGIC
 * @version:	version of the partition table
 * @num_entries: number of partitions in the table
 * @reserved:	for now reserved entries
 * @entry:	list of @smem_ptable_entry for the @num_entries partitions
 */
struct smem_ptable {
163 164 165 166
	u8 magic[4];
	__le32 version;
	__le32 num_entries;
	__le32 reserved[5];
167 168
	struct smem_ptable_entry entry[];
};
169 170

static const u8 SMEM_PTABLE_MAGIC[] = { 0x24, 0x54, 0x4f, 0x43 }; /* "$TOC" */
171 172 173 174 175 176 177 178 179 180 181 182 183 184

/**
 * struct smem_partition_header - header of the partitions
 * @magic:	magic number, must be SMEM_PART_MAGIC
 * @host0:	first processor/host with access to this partition
 * @host1:	second processor/host with access to this partition
 * @size:	size of the partition
 * @offset_free_uncached: offset to the first free byte of uncached memory in
 *		this partition
 * @offset_free_cached: offset to the first free byte of cached memory in this
 *		partition
 * @reserved:	for now reserved entries
 */
struct smem_partition_header {
185 186 187 188 189 190 191
	u8 magic[4];
	__le16 host0;
	__le16 host1;
	__le32 size;
	__le32 offset_free_uncached;
	__le32 offset_free_cached;
	__le32 reserved[3];
192
};
193 194

static const u8 SMEM_PART_MAGIC[] = { 0x24, 0x50, 0x52, 0x54 };
195 196 197 198 199 200 201 202 203 204 205

/**
 * struct smem_private_entry - header of each item in the private partition
 * @canary:	magic number, must be SMEM_PRIVATE_CANARY
 * @item:	identifying number of the smem item
 * @size:	size of the data, including padding bytes
 * @padding_data: number of bytes of padding of data
 * @padding_hdr: number of bytes of padding between the header and the data
 * @reserved:	for now reserved entry
 */
struct smem_private_entry {
206 207 208 209 210 211
	u16 canary; /* bytes are the same so no swapping needed */
	__le16 item;
	__le32 size; /* includes padding bytes */
	__le16 padding_data;
	__le16 padding_hdr;
	__le32 reserved;
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
};
#define SMEM_PRIVATE_CANARY	0xa5a5

/**
 * struct smem_region - representation of a chunk of memory used for smem
 * @aux_base:	identifier of aux_mem base
 * @virt_base:	virtual base address of memory with this aux_mem identifier
 * @size:	size of the memory region
 */
struct smem_region {
	u32 aux_base;
	void __iomem *virt_base;
	size_t size;
};

/**
 * struct qcom_smem - device data for the smem device
 * @dev:	device pointer
 * @hwlock:	reference to a hwspinlock
 * @partitions:	list of pointers to partitions affecting the current
 *		processor/host
 * @num_regions: number of @regions
 * @regions:	list of the memory regions defining the shared memory
 */
struct qcom_smem {
	struct device *dev;

	struct hwspinlock *hwlock;

	struct smem_partition_header *partitions[SMEM_HOST_COUNT];

	unsigned num_regions;
	struct smem_region regions[0];
};

247
static struct smem_private_entry *
248
phdr_to_last_uncached_entry(struct smem_partition_header *phdr)
249 250 251 252 253 254
{
	void *p = phdr;

	return p + le32_to_cpu(phdr->offset_free_uncached);
}

255
static void *phdr_to_last_cached_entry(struct smem_partition_header *phdr)
256 257 258 259 260 261 262
{
	void *p = phdr;

	return p + le32_to_cpu(phdr->offset_free_cached);
}

static struct smem_private_entry *
263
phdr_to_first_uncached_entry(struct smem_partition_header *phdr)
264 265 266 267 268 269 270
{
	void *p = phdr;

	return p + sizeof(*phdr);
}

static struct smem_private_entry *
271
uncached_entry_next(struct smem_private_entry *e)
272 273 274 275 276 277 278
{
	void *p = e;

	return p + sizeof(*e) + le16_to_cpu(e->padding_hdr) +
	       le32_to_cpu(e->size);
}

279
static void *uncached_entry_to_item(struct smem_private_entry *e)
280 281 282 283 284 285
{
	void *p = e;

	return p + sizeof(*e) + le16_to_cpu(e->padding_hdr);
}

286 287 288 289 290 291 292 293 294 295 296 297
/* Pointer to the one and only smem handle */
static struct qcom_smem *__smem;

/* Timeout (ms) for the trylock of remote spinlocks */
#define HWSPINLOCK_TIMEOUT	1000

static int qcom_smem_alloc_private(struct qcom_smem *smem,
				   unsigned host,
				   unsigned item,
				   size_t size)
{
	struct smem_partition_header *phdr;
298
	struct smem_private_entry *hdr, *end;
299
	size_t alloc_size;
300
	void *cached;
301 302

	phdr = smem->partitions[host];
303 304 305
	hdr = phdr_to_first_uncached_entry(phdr);
	end = phdr_to_last_uncached_entry(phdr);
	cached = phdr_to_last_cached_entry(phdr);
306

307
	while (hdr < end) {
308 309 310 311 312 313 314
		if (hdr->canary != SMEM_PRIVATE_CANARY) {
			dev_err(smem->dev,
				"Found invalid canary in host %d partition\n",
				host);
			return -EINVAL;
		}

315
		if (le16_to_cpu(hdr->item) == item)
316 317
			return -EEXIST;

318
		hdr = uncached_entry_next(hdr);
319 320 321 322
	}

	/* Check that we don't grow into the cached region */
	alloc_size = sizeof(*hdr) + ALIGN(size, 8);
323
	if ((void *)hdr + alloc_size >= cached) {
324 325 326 327 328
		dev_err(smem->dev, "Out of memory\n");
		return -ENOSPC;
	}

	hdr->canary = SMEM_PRIVATE_CANARY;
329 330 331
	hdr->item = cpu_to_le16(item);
	hdr->size = cpu_to_le32(ALIGN(size, 8));
	hdr->padding_data = cpu_to_le16(le32_to_cpu(hdr->size) - size);
332 333 334 335 336 337 338 339
	hdr->padding_hdr = 0;

	/*
	 * Ensure the header is written before we advance the free offset, so
	 * that remote processors that does not take the remote spinlock still
	 * gets a consistent view of the linked list.
	 */
	wmb();
340
	le32_add_cpu(&phdr->offset_free_uncached, alloc_size);
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360

	return 0;
}

static int qcom_smem_alloc_global(struct qcom_smem *smem,
				  unsigned item,
				  size_t size)
{
	struct smem_header *header;
	struct smem_global_entry *entry;

	if (WARN_ON(item >= SMEM_ITEM_COUNT))
		return -EINVAL;

	header = smem->regions[0].virt_base;
	entry = &header->toc[item];
	if (entry->allocated)
		return -EEXIST;

	size = ALIGN(size, 8);
361
	if (WARN_ON(size > le32_to_cpu(header->available)))
362 363 364
		return -ENOMEM;

	entry->offset = header->free_offset;
365
	entry->size = cpu_to_le32(size);
366 367 368 369 370 371 372

	/*
	 * Ensure the header is consistent before we mark the item allocated,
	 * so that remote processors will get a consistent view of the item
	 * even though they do not take the spinlock on read.
	 */
	wmb();
373
	entry->allocated = cpu_to_le32(1);
374

375 376
	le32_add_cpu(&header->free_offset, size);
	le32_add_cpu(&header->available, -size);
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

	return 0;
}

/**
 * qcom_smem_alloc() - allocate space for a smem item
 * @host:	remote processor id, or -1
 * @item:	smem item handle
 * @size:	number of bytes to be allocated
 *
 * Allocate space for a given smem item of size @size, given that the item is
 * not yet allocated.
 */
int qcom_smem_alloc(unsigned host, unsigned item, size_t size)
{
	unsigned long flags;
	int ret;

	if (!__smem)
		return -EPROBE_DEFER;

	if (item < SMEM_ITEM_LAST_FIXED) {
		dev_err(__smem->dev,
			"Rejecting allocation of static entry %d\n", item);
		return -EINVAL;
	}

	ret = hwspin_lock_timeout_irqsave(__smem->hwlock,
					  HWSPINLOCK_TIMEOUT,
					  &flags);
	if (ret)
		return ret;

410 411 412
	if (host < SMEM_HOST_COUNT && __smem->partitions[host])
		ret = qcom_smem_alloc_private(__smem, host, item, size);
	else
413 414 415 416 417 418 419 420
		ret = qcom_smem_alloc_global(__smem, item, size);

	hwspin_unlock_irqrestore(__smem->hwlock, &flags);

	return ret;
}
EXPORT_SYMBOL(qcom_smem_alloc);

421 422 423
static void *qcom_smem_get_global(struct qcom_smem *smem,
				  unsigned item,
				  size_t *size)
424 425 426 427 428 429 430 431
{
	struct smem_header *header;
	struct smem_region *area;
	struct smem_global_entry *entry;
	u32 aux_base;
	unsigned i;

	if (WARN_ON(item >= SMEM_ITEM_COUNT))
432
		return ERR_PTR(-EINVAL);
433 434 435 436

	header = smem->regions[0].virt_base;
	entry = &header->toc[item];
	if (!entry->allocated)
437
		return ERR_PTR(-ENXIO);
438

439
	aux_base = le32_to_cpu(entry->aux_base) & AUX_BASE_MASK;
440

441 442
	for (i = 0; i < smem->num_regions; i++) {
		area = &smem->regions[i];
443

444 445
		if (area->aux_base == aux_base || !aux_base) {
			if (size != NULL)
446 447
				*size = le32_to_cpu(entry->size);
			return area->virt_base + le32_to_cpu(entry->offset);
448 449 450
		}
	}

451
	return ERR_PTR(-ENOENT);
452 453
}

454 455 456 457
static void *qcom_smem_get_private(struct qcom_smem *smem,
				   unsigned host,
				   unsigned item,
				   size_t *size)
458 459
{
	struct smem_partition_header *phdr;
460
	struct smem_private_entry *e, *end;
461 462

	phdr = smem->partitions[host];
463 464
	e = phdr_to_first_uncached_entry(phdr);
	end = phdr_to_last_uncached_entry(phdr);
465

466 467
	while (e < end) {
		if (e->canary != SMEM_PRIVATE_CANARY) {
468 469 470
			dev_err(smem->dev,
				"Found invalid canary in host %d partition\n",
				host);
471
			return ERR_PTR(-EINVAL);
472 473
		}

474
		if (le16_to_cpu(e->item) == item) {
475
			if (size != NULL)
476 477
				*size = le32_to_cpu(e->size) -
					le16_to_cpu(e->padding_data);
478

479
			return uncached_entry_to_item(e);
480 481
		}

482
		e = uncached_entry_next(e);
483 484
	}

485
	return ERR_PTR(-ENOENT);
486 487 488 489 490 491 492 493
}

/**
 * qcom_smem_get() - resolve ptr of size of a smem item
 * @host:	the remote processor, or -1
 * @item:	smem item handle
 * @size:	pointer to be filled out with size of the item
 *
494 495
 * Looks up smem item and returns pointer to it. Size of smem
 * item is returned in @size.
496
 */
497
void *qcom_smem_get(unsigned host, unsigned item, size_t *size)
498 499 500
{
	unsigned long flags;
	int ret;
501
	void *ptr = ERR_PTR(-EPROBE_DEFER);
502 503

	if (!__smem)
504
		return ptr;
505 506 507 508 509

	ret = hwspin_lock_timeout_irqsave(__smem->hwlock,
					  HWSPINLOCK_TIMEOUT,
					  &flags);
	if (ret)
510
		return ERR_PTR(ret);
511

512
	if (host < SMEM_HOST_COUNT && __smem->partitions[host])
513
		ptr = qcom_smem_get_private(__smem, host, item, size);
514
	else
515
		ptr = qcom_smem_get_global(__smem, item, size);
516 517

	hwspin_unlock_irqrestore(__smem->hwlock, &flags);
518 519

	return ptr;
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541

}
EXPORT_SYMBOL(qcom_smem_get);

/**
 * qcom_smem_get_free_space() - retrieve amount of free space in a partition
 * @host:	the remote processor identifying a partition, or -1
 *
 * To be used by smem clients as a quick way to determine if any new
 * allocations has been made.
 */
int qcom_smem_get_free_space(unsigned host)
{
	struct smem_partition_header *phdr;
	struct smem_header *header;
	unsigned ret;

	if (!__smem)
		return -EPROBE_DEFER;

	if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
		phdr = __smem->partitions[host];
542 543
		ret = le32_to_cpu(phdr->offset_free_cached) -
		      le32_to_cpu(phdr->offset_free_uncached);
544 545
	} else {
		header = __smem->regions[0].virt_base;
546
		ret = le32_to_cpu(header->available);
547 548 549 550 551 552 553 554
	}

	return ret;
}
EXPORT_SYMBOL(qcom_smem_get_free_space);

static int qcom_smem_get_sbl_version(struct qcom_smem *smem)
{
555
	__le32 *versions;
556 557
	size_t size;

558 559
	versions = qcom_smem_get_global(smem, SMEM_ITEM_VERSION, &size);
	if (IS_ERR(versions)) {
560 561 562 563 564 565 566 567 568
		dev_err(smem->dev, "Unable to read the version item\n");
		return -ENOENT;
	}

	if (size < sizeof(unsigned) * SMEM_MASTER_SBL_VERSION_INDEX) {
		dev_err(smem->dev, "Version item is too small\n");
		return -EINVAL;
	}

569
	return le32_to_cpu(versions[SMEM_MASTER_SBL_VERSION_INDEX]);
570 571 572 573 574 575 576 577 578
}

static int qcom_smem_enumerate_partitions(struct qcom_smem *smem,
					  unsigned local_host)
{
	struct smem_partition_header *header;
	struct smem_ptable_entry *entry;
	struct smem_ptable *ptable;
	unsigned remote_host;
579
	u32 version, host0, host1;
580 581 582
	int i;

	ptable = smem->regions[0].virt_base + smem->regions[0].size - SZ_4K;
583
	if (memcmp(ptable->magic, SMEM_PTABLE_MAGIC, sizeof(ptable->magic)))
584 585
		return 0;

586 587
	version = le32_to_cpu(ptable->version);
	if (version != 1) {
588
		dev_err(smem->dev,
589
			"Unsupported partition header version %d\n", version);
590 591 592
		return -EINVAL;
	}

593
	for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
594
		entry = &ptable->entry[i];
595 596
		host0 = le16_to_cpu(entry->host0);
		host1 = le16_to_cpu(entry->host1);
597

598
		if (host0 != local_host && host1 != local_host)
599 600
			continue;

601
		if (!le32_to_cpu(entry->offset))
602 603
			continue;

604
		if (!le32_to_cpu(entry->size))
605 606
			continue;

607 608
		if (host0 == local_host)
			remote_host = host1;
609
		else
610
			remote_host = host0;
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625

		if (remote_host >= SMEM_HOST_COUNT) {
			dev_err(smem->dev,
				"Invalid remote host %d\n",
				remote_host);
			return -EINVAL;
		}

		if (smem->partitions[remote_host]) {
			dev_err(smem->dev,
				"Already found a partition for host %d\n",
				remote_host);
			return -EINVAL;
		}

626 627 628
		header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
		host0 = le16_to_cpu(header->host0);
		host1 = le16_to_cpu(header->host1);
629

630 631
		if (memcmp(header->magic, SMEM_PART_MAGIC,
			    sizeof(header->magic))) {
632 633 634 635 636
			dev_err(smem->dev,
				"Partition %d has invalid magic\n", i);
			return -EINVAL;
		}

637
		if (host0 != local_host && host1 != local_host) {
638 639 640 641 642
			dev_err(smem->dev,
				"Partition %d hosts are invalid\n", i);
			return -EINVAL;
		}

643
		if (host0 != remote_host && host1 != remote_host) {
644 645 646 647 648 649 650 651 652 653 654
			dev_err(smem->dev,
				"Partition %d hosts are invalid\n", i);
			return -EINVAL;
		}

		if (header->size != entry->size) {
			dev_err(smem->dev,
				"Partition %d has invalid size\n", i);
			return -EINVAL;
		}

655
		if (le32_to_cpu(header->offset_free_uncached) > le32_to_cpu(header->size)) {
656 657 658 659 660 661 662 663 664 665 666
			dev_err(smem->dev,
				"Partition %d has invalid free pointer\n", i);
			return -EINVAL;
		}

		smem->partitions[remote_host] = header;
	}

	return 0;
}

667 668
static int qcom_smem_map_memory(struct qcom_smem *smem, struct device *dev,
				const char *name, int i)
669
{
670 671 672
	struct device_node *np;
	struct resource r;
	int ret;
673

674 675 676 677
	np = of_parse_phandle(dev->of_node, name, 0);
	if (!np) {
		dev_err(dev, "No %s specified\n", name);
		return -EINVAL;
678 679
	}

680 681 682 683 684 685 686
	ret = of_address_to_resource(np, 0, &r);
	of_node_put(np);
	if (ret)
		return ret;

	smem->regions[i].aux_base = (u32)r.start;
	smem->regions[i].size = resource_size(&r);
687
	smem->regions[i].virt_base = devm_ioremap_wc(dev, r.start, resource_size(&r));
688 689 690 691
	if (!smem->regions[i].virt_base)
		return -ENOMEM;

	return 0;
692 693 694 695 696 697 698
}

static int qcom_smem_probe(struct platform_device *pdev)
{
	struct smem_header *header;
	struct qcom_smem *smem;
	size_t array_size;
699
	int num_regions;
700 701 702 703
	int hwlock_id;
	u32 version;
	int ret;

704 705 706
	num_regions = 1;
	if (of_find_property(pdev->dev.of_node, "qcom,rpm-msg-ram", NULL))
		num_regions++;
707 708 709 710 711 712 713 714 715

	array_size = num_regions * sizeof(struct smem_region);
	smem = devm_kzalloc(&pdev->dev, sizeof(*smem) + array_size, GFP_KERNEL);
	if (!smem)
		return -ENOMEM;

	smem->dev = &pdev->dev;
	smem->num_regions = num_regions;

716
	ret = qcom_smem_map_memory(smem, &pdev->dev, "memory-region", 0);
717 718 719
	if (ret)
		return ret;

720 721 722
	if (num_regions > 1 && (ret = qcom_smem_map_memory(smem, &pdev->dev,
					"qcom,rpm-msg-ram", 1)))
		return ret;
723 724

	header = smem->regions[0].virt_base;
725 726
	if (le32_to_cpu(header->initialized) != 1 ||
	    le32_to_cpu(header->reserved)) {
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
		dev_err(&pdev->dev, "SMEM is not initialized by SBL\n");
		return -EINVAL;
	}

	version = qcom_smem_get_sbl_version(smem);
	if (version >> 16 != SMEM_EXPECTED_VERSION) {
		dev_err(&pdev->dev, "Unsupported SMEM version 0x%x\n", version);
		return -EINVAL;
	}

	ret = qcom_smem_enumerate_partitions(smem, SMEM_HOST_APPS);
	if (ret < 0)
		return ret;

	hwlock_id = of_hwspin_lock_get_id(pdev->dev.of_node, 0);
	if (hwlock_id < 0) {
743 744
		if (hwlock_id != -EPROBE_DEFER)
			dev_err(&pdev->dev, "failed to retrieve hwlock\n");
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
		return hwlock_id;
	}

	smem->hwlock = hwspin_lock_request_specific(hwlock_id);
	if (!smem->hwlock)
		return -ENXIO;

	__smem = smem;

	return 0;
}

static int qcom_smem_remove(struct platform_device *pdev)
{
	hwspin_lock_free(__smem->hwlock);
760
	__smem = NULL;
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

	return 0;
}

static const struct of_device_id qcom_smem_of_match[] = {
	{ .compatible = "qcom,smem" },
	{}
};
MODULE_DEVICE_TABLE(of, qcom_smem_of_match);

static struct platform_driver qcom_smem_driver = {
	.probe = qcom_smem_probe,
	.remove = qcom_smem_remove,
	.driver  = {
		.name = "qcom-smem",
		.of_match_table = qcom_smem_of_match,
		.suppress_bind_attrs = true,
	},
};

static int __init qcom_smem_init(void)
{
	return platform_driver_register(&qcom_smem_driver);
}
arch_initcall(qcom_smem_init);

static void __exit qcom_smem_exit(void)
{
	platform_driver_unregister(&qcom_smem_driver);
}
module_exit(qcom_smem_exit)

MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
MODULE_DESCRIPTION("Qualcomm Shared Memory Manager");
MODULE_LICENSE("GPL v2");