smem.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. * Copyright (c) 2015, Sony Mobile Communications AB.
  3. * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 and
  7. * only version 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/hwspinlock.h>
  15. #include <linux/io.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <linux/soc/qcom/smem.h>
  22. /*
  23. * The Qualcomm shared memory system is a allocate only heap structure that
  24. * consists of one of more memory areas that can be accessed by the processors
  25. * in the SoC.
  26. *
  27. * All systems contains a global heap, accessible by all processors in the SoC,
  28. * with a table of contents data structure (@smem_header) at the beginning of
  29. * the main shared memory block.
  30. *
  31. * The global header contains meta data for allocations as well as a fixed list
  32. * of 512 entries (@smem_global_entry) that can be initialized to reference
  33. * parts of the shared memory space.
  34. *
  35. *
  36. * In addition to this global heap a set of "private" heaps can be set up at
  37. * boot time with access restrictions so that only certain processor pairs can
  38. * access the data.
  39. *
  40. * These partitions are referenced from an optional partition table
  41. * (@smem_ptable), that is found 4kB from the end of the main smem region. The
  42. * partition table entries (@smem_ptable_entry) lists the involved processors
  43. * (or hosts) and their location in the main shared memory region.
  44. *
  45. * Each partition starts with a header (@smem_partition_header) that identifies
  46. * the partition and holds properties for the two internal memory regions. The
  47. * two regions are cached and non-cached memory respectively. Each region
  48. * contain a link list of allocation headers (@smem_private_entry) followed by
  49. * their data.
  50. *
  51. * Items in the non-cached region are allocated from the start of the partition
  52. * while items in the cached region are allocated from the end. The free area
  53. * is hence the region between the cached and non-cached offsets.
  54. *
  55. *
  56. * To synchronize allocations in the shared memory heaps a remote spinlock must
  57. * be held - currently lock number 3 of the sfpb or tcsr is used for this on all
  58. * platforms.
  59. *
  60. */
  61. /*
  62. * Item 3 of the global heap contains an array of versions for the various
  63. * software components in the SoC. We verify that the boot loader version is
  64. * what the expected version (SMEM_EXPECTED_VERSION) as a sanity check.
  65. */
  66. #define SMEM_ITEM_VERSION 3
  67. #define SMEM_MASTER_SBL_VERSION_INDEX 7
  68. #define SMEM_EXPECTED_VERSION 11
  69. /*
  70. * The first 8 items are only to be allocated by the boot loader while
  71. * initializing the heap.
  72. */
  73. #define SMEM_ITEM_LAST_FIXED 8
  74. /* Highest accepted item number, for both global and private heaps */
  75. #define SMEM_ITEM_COUNT 512
  76. /* Processor/host identifier for the application processor */
  77. #define SMEM_HOST_APPS 0
  78. /* Max number of processors/hosts in a system */
  79. #define SMEM_HOST_COUNT 9
  80. /**
  81. * struct smem_proc_comm - proc_comm communication struct (legacy)
  82. * @command: current command to be executed
  83. * @status: status of the currently requested command
  84. * @params: parameters to the command
  85. */
  86. struct smem_proc_comm {
  87. u32 command;
  88. u32 status;
  89. u32 params[2];
  90. };
  91. /**
  92. * struct smem_global_entry - entry to reference smem items on the heap
  93. * @allocated: boolean to indicate if this entry is used
  94. * @offset: offset to the allocated space
  95. * @size: size of the allocated space, 8 byte aligned
  96. * @aux_base: base address for the memory region used by this unit, or 0 for
  97. * the default region. bits 0,1 are reserved
  98. */
  99. struct smem_global_entry {
  100. u32 allocated;
  101. u32 offset;
  102. u32 size;
  103. u32 aux_base; /* bits 1:0 reserved */
  104. };
  105. #define AUX_BASE_MASK 0xfffffffc
  106. /**
  107. * struct smem_header - header found in beginning of primary smem region
  108. * @proc_comm: proc_comm communication interface (legacy)
  109. * @version: array of versions for the various subsystems
  110. * @initialized: boolean to indicate that smem is initialized
  111. * @free_offset: index of the first unallocated byte in smem
  112. * @available: number of bytes available for allocation
  113. * @reserved: reserved field, must be 0
  114. * toc: array of references to items
  115. */
  116. struct smem_header {
  117. struct smem_proc_comm proc_comm[4];
  118. u32 version[32];
  119. u32 initialized;
  120. u32 free_offset;
  121. u32 available;
  122. u32 reserved;
  123. struct smem_global_entry toc[SMEM_ITEM_COUNT];
  124. };
  125. /**
  126. * struct smem_ptable_entry - one entry in the @smem_ptable list
  127. * @offset: offset, within the main shared memory region, of the partition
  128. * @size: size of the partition
  129. * @flags: flags for the partition (currently unused)
  130. * @host0: first processor/host with access to this partition
  131. * @host1: second processor/host with access to this partition
  132. * @reserved: reserved entries for later use
  133. */
  134. struct smem_ptable_entry {
  135. u32 offset;
  136. u32 size;
  137. u32 flags;
  138. u16 host0;
  139. u16 host1;
  140. u32 reserved[8];
  141. };
  142. /**
  143. * struct smem_ptable - partition table for the private partitions
  144. * @magic: magic number, must be SMEM_PTABLE_MAGIC
  145. * @version: version of the partition table
  146. * @num_entries: number of partitions in the table
  147. * @reserved: for now reserved entries
  148. * @entry: list of @smem_ptable_entry for the @num_entries partitions
  149. */
  150. struct smem_ptable {
  151. u32 magic;
  152. u32 version;
  153. u32 num_entries;
  154. u32 reserved[5];
  155. struct smem_ptable_entry entry[];
  156. };
  157. #define SMEM_PTABLE_MAGIC 0x434f5424 /* "$TOC" */
  158. /**
  159. * struct smem_partition_header - header of the partitions
  160. * @magic: magic number, must be SMEM_PART_MAGIC
  161. * @host0: first processor/host with access to this partition
  162. * @host1: second processor/host with access to this partition
  163. * @size: size of the partition
  164. * @offset_free_uncached: offset to the first free byte of uncached memory in
  165. * this partition
  166. * @offset_free_cached: offset to the first free byte of cached memory in this
  167. * partition
  168. * @reserved: for now reserved entries
  169. */
  170. struct smem_partition_header {
  171. u32 magic;
  172. u16 host0;
  173. u16 host1;
  174. u32 size;
  175. u32 offset_free_uncached;
  176. u32 offset_free_cached;
  177. u32 reserved[3];
  178. };
  179. #define SMEM_PART_MAGIC 0x54525024 /* "$PRT" */
  180. /**
  181. * struct smem_private_entry - header of each item in the private partition
  182. * @canary: magic number, must be SMEM_PRIVATE_CANARY
  183. * @item: identifying number of the smem item
  184. * @size: size of the data, including padding bytes
  185. * @padding_data: number of bytes of padding of data
  186. * @padding_hdr: number of bytes of padding between the header and the data
  187. * @reserved: for now reserved entry
  188. */
  189. struct smem_private_entry {
  190. u16 canary;
  191. u16 item;
  192. u32 size; /* includes padding bytes */
  193. u16 padding_data;
  194. u16 padding_hdr;
  195. u32 reserved;
  196. };
  197. #define SMEM_PRIVATE_CANARY 0xa5a5
  198. /**
  199. * struct smem_region - representation of a chunk of memory used for smem
  200. * @aux_base: identifier of aux_mem base
  201. * @virt_base: virtual base address of memory with this aux_mem identifier
  202. * @size: size of the memory region
  203. */
  204. struct smem_region {
  205. u32 aux_base;
  206. void __iomem *virt_base;
  207. size_t size;
  208. };
  209. /**
  210. * struct qcom_smem - device data for the smem device
  211. * @dev: device pointer
  212. * @hwlock: reference to a hwspinlock
  213. * @partitions: list of pointers to partitions affecting the current
  214. * processor/host
  215. * @num_regions: number of @regions
  216. * @regions: list of the memory regions defining the shared memory
  217. */
  218. struct qcom_smem {
  219. struct device *dev;
  220. struct hwspinlock *hwlock;
  221. struct smem_partition_header *partitions[SMEM_HOST_COUNT];
  222. unsigned num_regions;
  223. struct smem_region regions[0];
  224. };
  225. /* Pointer to the one and only smem handle */
  226. static struct qcom_smem *__smem;
  227. /* Timeout (ms) for the trylock of remote spinlocks */
  228. #define HWSPINLOCK_TIMEOUT 1000
  229. static int qcom_smem_alloc_private(struct qcom_smem *smem,
  230. unsigned host,
  231. unsigned item,
  232. size_t size)
  233. {
  234. struct smem_partition_header *phdr;
  235. struct smem_private_entry *hdr;
  236. size_t alloc_size;
  237. void *p;
  238. phdr = smem->partitions[host];
  239. p = (void *)phdr + sizeof(*phdr);
  240. while (p < (void *)phdr + phdr->offset_free_uncached) {
  241. hdr = p;
  242. if (hdr->canary != SMEM_PRIVATE_CANARY) {
  243. dev_err(smem->dev,
  244. "Found invalid canary in host %d partition\n",
  245. host);
  246. return -EINVAL;
  247. }
  248. if (hdr->item == item)
  249. return -EEXIST;
  250. p += sizeof(*hdr) + hdr->padding_hdr + hdr->size;
  251. }
  252. /* Check that we don't grow into the cached region */
  253. alloc_size = sizeof(*hdr) + ALIGN(size, 8);
  254. if (p + alloc_size >= (void *)phdr + phdr->offset_free_cached) {
  255. dev_err(smem->dev, "Out of memory\n");
  256. return -ENOSPC;
  257. }
  258. hdr = p;
  259. hdr->canary = SMEM_PRIVATE_CANARY;
  260. hdr->item = item;
  261. hdr->size = ALIGN(size, 8);
  262. hdr->padding_data = hdr->size - size;
  263. hdr->padding_hdr = 0;
  264. /*
  265. * Ensure the header is written before we advance the free offset, so
  266. * that remote processors that does not take the remote spinlock still
  267. * gets a consistent view of the linked list.
  268. */
  269. wmb();
  270. phdr->offset_free_uncached += alloc_size;
  271. return 0;
  272. }
  273. static int qcom_smem_alloc_global(struct qcom_smem *smem,
  274. unsigned item,
  275. size_t size)
  276. {
  277. struct smem_header *header;
  278. struct smem_global_entry *entry;
  279. if (WARN_ON(item >= SMEM_ITEM_COUNT))
  280. return -EINVAL;
  281. header = smem->regions[0].virt_base;
  282. entry = &header->toc[item];
  283. if (entry->allocated)
  284. return -EEXIST;
  285. size = ALIGN(size, 8);
  286. if (WARN_ON(size > header->available))
  287. return -ENOMEM;
  288. entry->offset = header->free_offset;
  289. entry->size = size;
  290. /*
  291. * Ensure the header is consistent before we mark the item allocated,
  292. * so that remote processors will get a consistent view of the item
  293. * even though they do not take the spinlock on read.
  294. */
  295. wmb();
  296. entry->allocated = 1;
  297. header->free_offset += size;
  298. header->available -= size;
  299. return 0;
  300. }
  301. /**
  302. * qcom_smem_alloc() - allocate space for a smem item
  303. * @host: remote processor id, or -1
  304. * @item: smem item handle
  305. * @size: number of bytes to be allocated
  306. *
  307. * Allocate space for a given smem item of size @size, given that the item is
  308. * not yet allocated.
  309. */
  310. int qcom_smem_alloc(unsigned host, unsigned item, size_t size)
  311. {
  312. unsigned long flags;
  313. int ret;
  314. if (!__smem)
  315. return -EPROBE_DEFER;
  316. if (item < SMEM_ITEM_LAST_FIXED) {
  317. dev_err(__smem->dev,
  318. "Rejecting allocation of static entry %d\n", item);
  319. return -EINVAL;
  320. }
  321. ret = hwspin_lock_timeout_irqsave(__smem->hwlock,
  322. HWSPINLOCK_TIMEOUT,
  323. &flags);
  324. if (ret)
  325. return ret;
  326. if (host < SMEM_HOST_COUNT && __smem->partitions[host])
  327. ret = qcom_smem_alloc_private(__smem, host, item, size);
  328. else
  329. ret = qcom_smem_alloc_global(__smem, item, size);
  330. hwspin_unlock_irqrestore(__smem->hwlock, &flags);
  331. return ret;
  332. }
  333. EXPORT_SYMBOL(qcom_smem_alloc);
  334. static int qcom_smem_get_global(struct qcom_smem *smem,
  335. unsigned item,
  336. void **ptr,
  337. size_t *size)
  338. {
  339. struct smem_header *header;
  340. struct smem_region *area;
  341. struct smem_global_entry *entry;
  342. u32 aux_base;
  343. unsigned i;
  344. if (WARN_ON(item >= SMEM_ITEM_COUNT))
  345. return -EINVAL;
  346. header = smem->regions[0].virt_base;
  347. entry = &header->toc[item];
  348. if (!entry->allocated)
  349. return -ENXIO;
  350. if (ptr != NULL) {
  351. aux_base = entry->aux_base & AUX_BASE_MASK;
  352. for (i = 0; i < smem->num_regions; i++) {
  353. area = &smem->regions[i];
  354. if (area->aux_base == aux_base || !aux_base) {
  355. *ptr = area->virt_base + entry->offset;
  356. break;
  357. }
  358. }
  359. }
  360. if (size != NULL)
  361. *size = entry->size;
  362. return 0;
  363. }
  364. static int qcom_smem_get_private(struct qcom_smem *smem,
  365. unsigned host,
  366. unsigned item,
  367. void **ptr,
  368. size_t *size)
  369. {
  370. struct smem_partition_header *phdr;
  371. struct smem_private_entry *hdr;
  372. void *p;
  373. phdr = smem->partitions[host];
  374. p = (void *)phdr + sizeof(*phdr);
  375. while (p < (void *)phdr + phdr->offset_free_uncached) {
  376. hdr = p;
  377. if (hdr->canary != SMEM_PRIVATE_CANARY) {
  378. dev_err(smem->dev,
  379. "Found invalid canary in host %d partition\n",
  380. host);
  381. return -EINVAL;
  382. }
  383. if (hdr->item == item) {
  384. if (ptr != NULL)
  385. *ptr = p + sizeof(*hdr) + hdr->padding_hdr;
  386. if (size != NULL)
  387. *size = hdr->size - hdr->padding_data;
  388. return 0;
  389. }
  390. p += sizeof(*hdr) + hdr->padding_hdr + hdr->size;
  391. }
  392. return -ENOENT;
  393. }
  394. /**
  395. * qcom_smem_get() - resolve ptr of size of a smem item
  396. * @host: the remote processor, or -1
  397. * @item: smem item handle
  398. * @ptr: pointer to be filled out with address of the item
  399. * @size: pointer to be filled out with size of the item
  400. *
  401. * Looks up pointer and size of a smem item.
  402. */
  403. int qcom_smem_get(unsigned host, unsigned item, void **ptr, size_t *size)
  404. {
  405. unsigned long flags;
  406. int ret;
  407. if (!__smem)
  408. return -EPROBE_DEFER;
  409. ret = hwspin_lock_timeout_irqsave(__smem->hwlock,
  410. HWSPINLOCK_TIMEOUT,
  411. &flags);
  412. if (ret)
  413. return ret;
  414. if (host < SMEM_HOST_COUNT && __smem->partitions[host])
  415. ret = qcom_smem_get_private(__smem, host, item, ptr, size);
  416. else
  417. ret = qcom_smem_get_global(__smem, item, ptr, size);
  418. hwspin_unlock_irqrestore(__smem->hwlock, &flags);
  419. return ret;
  420. }
  421. EXPORT_SYMBOL(qcom_smem_get);
  422. /**
  423. * qcom_smem_get_free_space() - retrieve amount of free space in a partition
  424. * @host: the remote processor identifying a partition, or -1
  425. *
  426. * To be used by smem clients as a quick way to determine if any new
  427. * allocations has been made.
  428. */
  429. int qcom_smem_get_free_space(unsigned host)
  430. {
  431. struct smem_partition_header *phdr;
  432. struct smem_header *header;
  433. unsigned ret;
  434. if (!__smem)
  435. return -EPROBE_DEFER;
  436. if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
  437. phdr = __smem->partitions[host];
  438. ret = phdr->offset_free_cached - phdr->offset_free_uncached;
  439. } else {
  440. header = __smem->regions[0].virt_base;
  441. ret = header->available;
  442. }
  443. return ret;
  444. }
  445. EXPORT_SYMBOL(qcom_smem_get_free_space);
  446. static int qcom_smem_get_sbl_version(struct qcom_smem *smem)
  447. {
  448. unsigned *versions;
  449. size_t size;
  450. int ret;
  451. ret = qcom_smem_get_global(smem, SMEM_ITEM_VERSION,
  452. (void **)&versions, &size);
  453. if (ret < 0) {
  454. dev_err(smem->dev, "Unable to read the version item\n");
  455. return -ENOENT;
  456. }
  457. if (size < sizeof(unsigned) * SMEM_MASTER_SBL_VERSION_INDEX) {
  458. dev_err(smem->dev, "Version item is too small\n");
  459. return -EINVAL;
  460. }
  461. return versions[SMEM_MASTER_SBL_VERSION_INDEX];
  462. }
  463. static int qcom_smem_enumerate_partitions(struct qcom_smem *smem,
  464. unsigned local_host)
  465. {
  466. struct smem_partition_header *header;
  467. struct smem_ptable_entry *entry;
  468. struct smem_ptable *ptable;
  469. unsigned remote_host;
  470. int i;
  471. ptable = smem->regions[0].virt_base + smem->regions[0].size - SZ_4K;
  472. if (ptable->magic != SMEM_PTABLE_MAGIC)
  473. return 0;
  474. if (ptable->version != 1) {
  475. dev_err(smem->dev,
  476. "Unsupported partition header version %d\n",
  477. ptable->version);
  478. return -EINVAL;
  479. }
  480. for (i = 0; i < ptable->num_entries; i++) {
  481. entry = &ptable->entry[i];
  482. if (entry->host0 != local_host && entry->host1 != local_host)
  483. continue;
  484. if (!entry->offset)
  485. continue;
  486. if (!entry->size)
  487. continue;
  488. if (entry->host0 == local_host)
  489. remote_host = entry->host1;
  490. else
  491. remote_host = entry->host0;
  492. if (remote_host >= SMEM_HOST_COUNT) {
  493. dev_err(smem->dev,
  494. "Invalid remote host %d\n",
  495. remote_host);
  496. return -EINVAL;
  497. }
  498. if (smem->partitions[remote_host]) {
  499. dev_err(smem->dev,
  500. "Already found a partition for host %d\n",
  501. remote_host);
  502. return -EINVAL;
  503. }
  504. header = smem->regions[0].virt_base + entry->offset;
  505. if (header->magic != SMEM_PART_MAGIC) {
  506. dev_err(smem->dev,
  507. "Partition %d has invalid magic\n", i);
  508. return -EINVAL;
  509. }
  510. if (header->host0 != local_host && header->host1 != local_host) {
  511. dev_err(smem->dev,
  512. "Partition %d hosts are invalid\n", i);
  513. return -EINVAL;
  514. }
  515. if (header->host0 != remote_host && header->host1 != remote_host) {
  516. dev_err(smem->dev,
  517. "Partition %d hosts are invalid\n", i);
  518. return -EINVAL;
  519. }
  520. if (header->size != entry->size) {
  521. dev_err(smem->dev,
  522. "Partition %d has invalid size\n", i);
  523. return -EINVAL;
  524. }
  525. if (header->offset_free_uncached > header->size) {
  526. dev_err(smem->dev,
  527. "Partition %d has invalid free pointer\n", i);
  528. return -EINVAL;
  529. }
  530. smem->partitions[remote_host] = header;
  531. }
  532. return 0;
  533. }
  534. static int qcom_smem_count_mem_regions(struct platform_device *pdev)
  535. {
  536. struct resource *res;
  537. int num_regions = 0;
  538. int i;
  539. for (i = 0; i < pdev->num_resources; i++) {
  540. res = &pdev->resource[i];
  541. if (resource_type(res) == IORESOURCE_MEM)
  542. num_regions++;
  543. }
  544. return num_regions;
  545. }
  546. static int qcom_smem_probe(struct platform_device *pdev)
  547. {
  548. struct smem_header *header;
  549. struct device_node *np;
  550. struct qcom_smem *smem;
  551. struct resource *res;
  552. struct resource r;
  553. size_t array_size;
  554. int num_regions = 0;
  555. int hwlock_id;
  556. u32 version;
  557. int ret;
  558. int i;
  559. num_regions = qcom_smem_count_mem_regions(pdev) + 1;
  560. array_size = num_regions * sizeof(struct smem_region);
  561. smem = devm_kzalloc(&pdev->dev, sizeof(*smem) + array_size, GFP_KERNEL);
  562. if (!smem)
  563. return -ENOMEM;
  564. smem->dev = &pdev->dev;
  565. smem->num_regions = num_regions;
  566. np = of_parse_phandle(pdev->dev.of_node, "memory-region", 0);
  567. if (!np) {
  568. dev_err(&pdev->dev, "No memory-region specified\n");
  569. return -EINVAL;
  570. }
  571. ret = of_address_to_resource(np, 0, &r);
  572. of_node_put(np);
  573. if (ret)
  574. return ret;
  575. smem->regions[0].aux_base = (u32)r.start;
  576. smem->regions[0].size = resource_size(&r);
  577. smem->regions[0].virt_base = devm_ioremap_nocache(&pdev->dev,
  578. r.start,
  579. resource_size(&r));
  580. if (!smem->regions[0].virt_base)
  581. return -ENOMEM;
  582. for (i = 1; i < num_regions; i++) {
  583. res = platform_get_resource(pdev, IORESOURCE_MEM, i - 1);
  584. smem->regions[i].aux_base = (u32)res->start;
  585. smem->regions[i].size = resource_size(res);
  586. smem->regions[i].virt_base = devm_ioremap_nocache(&pdev->dev,
  587. res->start,
  588. resource_size(res));
  589. if (!smem->regions[i].virt_base)
  590. return -ENOMEM;
  591. }
  592. header = smem->regions[0].virt_base;
  593. if (header->initialized != 1 || header->reserved) {
  594. dev_err(&pdev->dev, "SMEM is not initialized by SBL\n");
  595. return -EINVAL;
  596. }
  597. version = qcom_smem_get_sbl_version(smem);
  598. if (version >> 16 != SMEM_EXPECTED_VERSION) {
  599. dev_err(&pdev->dev, "Unsupported SMEM version 0x%x\n", version);
  600. return -EINVAL;
  601. }
  602. ret = qcom_smem_enumerate_partitions(smem, SMEM_HOST_APPS);
  603. if (ret < 0)
  604. return ret;
  605. hwlock_id = of_hwspin_lock_get_id(pdev->dev.of_node, 0);
  606. if (hwlock_id < 0) {
  607. dev_err(&pdev->dev, "failed to retrieve hwlock\n");
  608. return hwlock_id;
  609. }
  610. smem->hwlock = hwspin_lock_request_specific(hwlock_id);
  611. if (!smem->hwlock)
  612. return -ENXIO;
  613. __smem = smem;
  614. return 0;
  615. }
  616. static int qcom_smem_remove(struct platform_device *pdev)
  617. {
  618. __smem = NULL;
  619. hwspin_lock_free(__smem->hwlock);
  620. return 0;
  621. }
  622. static const struct of_device_id qcom_smem_of_match[] = {
  623. { .compatible = "qcom,smem" },
  624. {}
  625. };
  626. MODULE_DEVICE_TABLE(of, qcom_smem_of_match);
  627. static struct platform_driver qcom_smem_driver = {
  628. .probe = qcom_smem_probe,
  629. .remove = qcom_smem_remove,
  630. .driver = {
  631. .name = "qcom-smem",
  632. .of_match_table = qcom_smem_of_match,
  633. .suppress_bind_attrs = true,
  634. },
  635. };
  636. static int __init qcom_smem_init(void)
  637. {
  638. return platform_driver_register(&qcom_smem_driver);
  639. }
  640. arch_initcall(qcom_smem_init);
  641. static void __exit qcom_smem_exit(void)
  642. {
  643. platform_driver_unregister(&qcom_smem_driver);
  644. }
  645. module_exit(qcom_smem_exit)
  646. MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
  647. MODULE_DESCRIPTION("Qualcomm Shared Memory Manager");
  648. MODULE_LICENSE("GPL v2");