smem.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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. __le32 command;
  88. __le32 status;
  89. __le32 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. __le32 allocated;
  101. __le32 offset;
  102. __le32 size;
  103. __le32 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. __le32 version[32];
  119. __le32 initialized;
  120. __le32 free_offset;
  121. __le32 available;
  122. __le32 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. __le32 offset;
  136. __le32 size;
  137. __le32 flags;
  138. __le16 host0;
  139. __le16 host1;
  140. __le32 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. u8 magic[4];
  152. __le32 version;
  153. __le32 num_entries;
  154. __le32 reserved[5];
  155. struct smem_ptable_entry entry[];
  156. };
  157. static const u8 SMEM_PTABLE_MAGIC[] = { 0x24, 0x54, 0x4f, 0x43 }; /* "$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. u8 magic[4];
  172. __le16 host0;
  173. __le16 host1;
  174. __le32 size;
  175. __le32 offset_free_uncached;
  176. __le32 offset_free_cached;
  177. __le32 reserved[3];
  178. };
  179. static const u8 SMEM_PART_MAGIC[] = { 0x24, 0x50, 0x52, 0x54 };
  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; /* bytes are the same so no swapping needed */
  191. __le16 item;
  192. __le32 size; /* includes padding bytes */
  193. __le16 padding_data;
  194. __le16 padding_hdr;
  195. __le32 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. static struct smem_private_entry *
  226. phdr_to_last_private_entry(struct smem_partition_header *phdr)
  227. {
  228. void *p = phdr;
  229. return p + le32_to_cpu(phdr->offset_free_uncached);
  230. }
  231. static void *phdr_to_first_cached_entry(struct smem_partition_header *phdr)
  232. {
  233. void *p = phdr;
  234. return p + le32_to_cpu(phdr->offset_free_cached);
  235. }
  236. static struct smem_private_entry *
  237. phdr_to_first_private_entry(struct smem_partition_header *phdr)
  238. {
  239. void *p = phdr;
  240. return p + sizeof(*phdr);
  241. }
  242. static struct smem_private_entry *
  243. private_entry_next(struct smem_private_entry *e)
  244. {
  245. void *p = e;
  246. return p + sizeof(*e) + le16_to_cpu(e->padding_hdr) +
  247. le32_to_cpu(e->size);
  248. }
  249. static void *entry_to_item(struct smem_private_entry *e)
  250. {
  251. void *p = e;
  252. return p + sizeof(*e) + le16_to_cpu(e->padding_hdr);
  253. }
  254. /* Pointer to the one and only smem handle */
  255. static struct qcom_smem *__smem;
  256. /* Timeout (ms) for the trylock of remote spinlocks */
  257. #define HWSPINLOCK_TIMEOUT 1000
  258. static int qcom_smem_alloc_private(struct qcom_smem *smem,
  259. unsigned host,
  260. unsigned item,
  261. size_t size)
  262. {
  263. struct smem_partition_header *phdr;
  264. struct smem_private_entry *hdr, *end;
  265. size_t alloc_size;
  266. void *cached;
  267. phdr = smem->partitions[host];
  268. hdr = phdr_to_first_private_entry(phdr);
  269. end = phdr_to_last_private_entry(phdr);
  270. cached = phdr_to_first_cached_entry(phdr);
  271. while (hdr < end) {
  272. if (hdr->canary != SMEM_PRIVATE_CANARY) {
  273. dev_err(smem->dev,
  274. "Found invalid canary in host %d partition\n",
  275. host);
  276. return -EINVAL;
  277. }
  278. if (le16_to_cpu(hdr->item) == item)
  279. return -EEXIST;
  280. hdr = private_entry_next(hdr);
  281. }
  282. /* Check that we don't grow into the cached region */
  283. alloc_size = sizeof(*hdr) + ALIGN(size, 8);
  284. if ((void *)hdr + alloc_size >= cached) {
  285. dev_err(smem->dev, "Out of memory\n");
  286. return -ENOSPC;
  287. }
  288. hdr->canary = SMEM_PRIVATE_CANARY;
  289. hdr->item = cpu_to_le16(item);
  290. hdr->size = cpu_to_le32(ALIGN(size, 8));
  291. hdr->padding_data = cpu_to_le16(le32_to_cpu(hdr->size) - size);
  292. hdr->padding_hdr = 0;
  293. /*
  294. * Ensure the header is written before we advance the free offset, so
  295. * that remote processors that does not take the remote spinlock still
  296. * gets a consistent view of the linked list.
  297. */
  298. wmb();
  299. le32_add_cpu(&phdr->offset_free_uncached, alloc_size);
  300. return 0;
  301. }
  302. static int qcom_smem_alloc_global(struct qcom_smem *smem,
  303. unsigned item,
  304. size_t size)
  305. {
  306. struct smem_header *header;
  307. struct smem_global_entry *entry;
  308. if (WARN_ON(item >= SMEM_ITEM_COUNT))
  309. return -EINVAL;
  310. header = smem->regions[0].virt_base;
  311. entry = &header->toc[item];
  312. if (entry->allocated)
  313. return -EEXIST;
  314. size = ALIGN(size, 8);
  315. if (WARN_ON(size > le32_to_cpu(header->available)))
  316. return -ENOMEM;
  317. entry->offset = header->free_offset;
  318. entry->size = cpu_to_le32(size);
  319. /*
  320. * Ensure the header is consistent before we mark the item allocated,
  321. * so that remote processors will get a consistent view of the item
  322. * even though they do not take the spinlock on read.
  323. */
  324. wmb();
  325. entry->allocated = cpu_to_le32(1);
  326. le32_add_cpu(&header->free_offset, size);
  327. le32_add_cpu(&header->available, -size);
  328. return 0;
  329. }
  330. /**
  331. * qcom_smem_alloc() - allocate space for a smem item
  332. * @host: remote processor id, or -1
  333. * @item: smem item handle
  334. * @size: number of bytes to be allocated
  335. *
  336. * Allocate space for a given smem item of size @size, given that the item is
  337. * not yet allocated.
  338. */
  339. int qcom_smem_alloc(unsigned host, unsigned item, size_t size)
  340. {
  341. unsigned long flags;
  342. int ret;
  343. if (!__smem)
  344. return -EPROBE_DEFER;
  345. if (item < SMEM_ITEM_LAST_FIXED) {
  346. dev_err(__smem->dev,
  347. "Rejecting allocation of static entry %d\n", item);
  348. return -EINVAL;
  349. }
  350. ret = hwspin_lock_timeout_irqsave(__smem->hwlock,
  351. HWSPINLOCK_TIMEOUT,
  352. &flags);
  353. if (ret)
  354. return ret;
  355. if (host < SMEM_HOST_COUNT && __smem->partitions[host])
  356. ret = qcom_smem_alloc_private(__smem, host, item, size);
  357. else
  358. ret = qcom_smem_alloc_global(__smem, item, size);
  359. hwspin_unlock_irqrestore(__smem->hwlock, &flags);
  360. return ret;
  361. }
  362. EXPORT_SYMBOL(qcom_smem_alloc);
  363. static void *qcom_smem_get_global(struct qcom_smem *smem,
  364. unsigned item,
  365. size_t *size)
  366. {
  367. struct smem_header *header;
  368. struct smem_region *area;
  369. struct smem_global_entry *entry;
  370. u32 aux_base;
  371. unsigned i;
  372. if (WARN_ON(item >= SMEM_ITEM_COUNT))
  373. return ERR_PTR(-EINVAL);
  374. header = smem->regions[0].virt_base;
  375. entry = &header->toc[item];
  376. if (!entry->allocated)
  377. return ERR_PTR(-ENXIO);
  378. aux_base = le32_to_cpu(entry->aux_base) & AUX_BASE_MASK;
  379. for (i = 0; i < smem->num_regions; i++) {
  380. area = &smem->regions[i];
  381. if (area->aux_base == aux_base || !aux_base) {
  382. if (size != NULL)
  383. *size = le32_to_cpu(entry->size);
  384. return area->virt_base + le32_to_cpu(entry->offset);
  385. }
  386. }
  387. return ERR_PTR(-ENOENT);
  388. }
  389. static void *qcom_smem_get_private(struct qcom_smem *smem,
  390. unsigned host,
  391. unsigned item,
  392. size_t *size)
  393. {
  394. struct smem_partition_header *phdr;
  395. struct smem_private_entry *e, *end;
  396. phdr = smem->partitions[host];
  397. e = phdr_to_first_private_entry(phdr);
  398. end = phdr_to_last_private_entry(phdr);
  399. while (e < end) {
  400. if (e->canary != SMEM_PRIVATE_CANARY) {
  401. dev_err(smem->dev,
  402. "Found invalid canary in host %d partition\n",
  403. host);
  404. return ERR_PTR(-EINVAL);
  405. }
  406. if (le16_to_cpu(e->item) == item) {
  407. if (size != NULL)
  408. *size = le32_to_cpu(e->size) -
  409. le16_to_cpu(e->padding_data);
  410. return entry_to_item(e);
  411. }
  412. e = private_entry_next(e);
  413. }
  414. return ERR_PTR(-ENOENT);
  415. }
  416. /**
  417. * qcom_smem_get() - resolve ptr of size of a smem item
  418. * @host: the remote processor, or -1
  419. * @item: smem item handle
  420. * @size: pointer to be filled out with size of the item
  421. *
  422. * Looks up smem item and returns pointer to it. Size of smem
  423. * item is returned in @size.
  424. */
  425. void *qcom_smem_get(unsigned host, unsigned item, size_t *size)
  426. {
  427. unsigned long flags;
  428. int ret;
  429. void *ptr = ERR_PTR(-EPROBE_DEFER);
  430. if (!__smem)
  431. return ptr;
  432. ret = hwspin_lock_timeout_irqsave(__smem->hwlock,
  433. HWSPINLOCK_TIMEOUT,
  434. &flags);
  435. if (ret)
  436. return ERR_PTR(ret);
  437. if (host < SMEM_HOST_COUNT && __smem->partitions[host])
  438. ptr = qcom_smem_get_private(__smem, host, item, size);
  439. else
  440. ptr = qcom_smem_get_global(__smem, item, size);
  441. hwspin_unlock_irqrestore(__smem->hwlock, &flags);
  442. return ptr;
  443. }
  444. EXPORT_SYMBOL(qcom_smem_get);
  445. /**
  446. * qcom_smem_get_free_space() - retrieve amount of free space in a partition
  447. * @host: the remote processor identifying a partition, or -1
  448. *
  449. * To be used by smem clients as a quick way to determine if any new
  450. * allocations has been made.
  451. */
  452. int qcom_smem_get_free_space(unsigned host)
  453. {
  454. struct smem_partition_header *phdr;
  455. struct smem_header *header;
  456. unsigned ret;
  457. if (!__smem)
  458. return -EPROBE_DEFER;
  459. if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
  460. phdr = __smem->partitions[host];
  461. ret = le32_to_cpu(phdr->offset_free_cached) -
  462. le32_to_cpu(phdr->offset_free_uncached);
  463. } else {
  464. header = __smem->regions[0].virt_base;
  465. ret = le32_to_cpu(header->available);
  466. }
  467. return ret;
  468. }
  469. EXPORT_SYMBOL(qcom_smem_get_free_space);
  470. static int qcom_smem_get_sbl_version(struct qcom_smem *smem)
  471. {
  472. __le32 *versions;
  473. size_t size;
  474. versions = qcom_smem_get_global(smem, SMEM_ITEM_VERSION, &size);
  475. if (IS_ERR(versions)) {
  476. dev_err(smem->dev, "Unable to read the version item\n");
  477. return -ENOENT;
  478. }
  479. if (size < sizeof(unsigned) * SMEM_MASTER_SBL_VERSION_INDEX) {
  480. dev_err(smem->dev, "Version item is too small\n");
  481. return -EINVAL;
  482. }
  483. return le32_to_cpu(versions[SMEM_MASTER_SBL_VERSION_INDEX]);
  484. }
  485. static int qcom_smem_enumerate_partitions(struct qcom_smem *smem,
  486. unsigned local_host)
  487. {
  488. struct smem_partition_header *header;
  489. struct smem_ptable_entry *entry;
  490. struct smem_ptable *ptable;
  491. unsigned remote_host;
  492. u32 version, host0, host1;
  493. int i;
  494. ptable = smem->regions[0].virt_base + smem->regions[0].size - SZ_4K;
  495. if (memcmp(ptable->magic, SMEM_PTABLE_MAGIC, sizeof(ptable->magic)))
  496. return 0;
  497. version = le32_to_cpu(ptable->version);
  498. if (version != 1) {
  499. dev_err(smem->dev,
  500. "Unsupported partition header version %d\n", version);
  501. return -EINVAL;
  502. }
  503. for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
  504. entry = &ptable->entry[i];
  505. host0 = le16_to_cpu(entry->host0);
  506. host1 = le16_to_cpu(entry->host1);
  507. if (host0 != local_host && host1 != local_host)
  508. continue;
  509. if (!le32_to_cpu(entry->offset))
  510. continue;
  511. if (!le32_to_cpu(entry->size))
  512. continue;
  513. if (host0 == local_host)
  514. remote_host = host1;
  515. else
  516. remote_host = host0;
  517. if (remote_host >= SMEM_HOST_COUNT) {
  518. dev_err(smem->dev,
  519. "Invalid remote host %d\n",
  520. remote_host);
  521. return -EINVAL;
  522. }
  523. if (smem->partitions[remote_host]) {
  524. dev_err(smem->dev,
  525. "Already found a partition for host %d\n",
  526. remote_host);
  527. return -EINVAL;
  528. }
  529. header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
  530. host0 = le16_to_cpu(header->host0);
  531. host1 = le16_to_cpu(header->host1);
  532. if (memcmp(header->magic, SMEM_PART_MAGIC,
  533. sizeof(header->magic))) {
  534. dev_err(smem->dev,
  535. "Partition %d has invalid magic\n", i);
  536. return -EINVAL;
  537. }
  538. if (host0 != local_host && host1 != local_host) {
  539. dev_err(smem->dev,
  540. "Partition %d hosts are invalid\n", i);
  541. return -EINVAL;
  542. }
  543. if (host0 != remote_host && host1 != remote_host) {
  544. dev_err(smem->dev,
  545. "Partition %d hosts are invalid\n", i);
  546. return -EINVAL;
  547. }
  548. if (header->size != entry->size) {
  549. dev_err(smem->dev,
  550. "Partition %d has invalid size\n", i);
  551. return -EINVAL;
  552. }
  553. if (le32_to_cpu(header->offset_free_uncached) > le32_to_cpu(header->size)) {
  554. dev_err(smem->dev,
  555. "Partition %d has invalid free pointer\n", i);
  556. return -EINVAL;
  557. }
  558. smem->partitions[remote_host] = header;
  559. }
  560. return 0;
  561. }
  562. static int qcom_smem_map_memory(struct qcom_smem *smem, struct device *dev,
  563. const char *name, int i)
  564. {
  565. struct device_node *np;
  566. struct resource r;
  567. int ret;
  568. np = of_parse_phandle(dev->of_node, name, 0);
  569. if (!np) {
  570. dev_err(dev, "No %s specified\n", name);
  571. return -EINVAL;
  572. }
  573. ret = of_address_to_resource(np, 0, &r);
  574. of_node_put(np);
  575. if (ret)
  576. return ret;
  577. smem->regions[i].aux_base = (u32)r.start;
  578. smem->regions[i].size = resource_size(&r);
  579. smem->regions[i].virt_base = devm_ioremap_nocache(dev, r.start,
  580. resource_size(&r));
  581. if (!smem->regions[i].virt_base)
  582. return -ENOMEM;
  583. return 0;
  584. }
  585. static int qcom_smem_probe(struct platform_device *pdev)
  586. {
  587. struct smem_header *header;
  588. struct qcom_smem *smem;
  589. size_t array_size;
  590. int num_regions;
  591. int hwlock_id;
  592. u32 version;
  593. int ret;
  594. num_regions = 1;
  595. if (of_find_property(pdev->dev.of_node, "qcom,rpm-msg-ram", NULL))
  596. num_regions++;
  597. array_size = num_regions * sizeof(struct smem_region);
  598. smem = devm_kzalloc(&pdev->dev, sizeof(*smem) + array_size, GFP_KERNEL);
  599. if (!smem)
  600. return -ENOMEM;
  601. smem->dev = &pdev->dev;
  602. smem->num_regions = num_regions;
  603. ret = qcom_smem_map_memory(smem, &pdev->dev, "memory-region", 0);
  604. if (ret)
  605. return ret;
  606. if (num_regions > 1 && (ret = qcom_smem_map_memory(smem, &pdev->dev,
  607. "qcom,rpm-msg-ram", 1)))
  608. return ret;
  609. header = smem->regions[0].virt_base;
  610. if (le32_to_cpu(header->initialized) != 1 ||
  611. le32_to_cpu(header->reserved)) {
  612. dev_err(&pdev->dev, "SMEM is not initialized by SBL\n");
  613. return -EINVAL;
  614. }
  615. version = qcom_smem_get_sbl_version(smem);
  616. if (version >> 16 != SMEM_EXPECTED_VERSION) {
  617. dev_err(&pdev->dev, "Unsupported SMEM version 0x%x\n", version);
  618. return -EINVAL;
  619. }
  620. ret = qcom_smem_enumerate_partitions(smem, SMEM_HOST_APPS);
  621. if (ret < 0)
  622. return ret;
  623. hwlock_id = of_hwspin_lock_get_id(pdev->dev.of_node, 0);
  624. if (hwlock_id < 0) {
  625. dev_err(&pdev->dev, "failed to retrieve hwlock\n");
  626. return hwlock_id;
  627. }
  628. smem->hwlock = hwspin_lock_request_specific(hwlock_id);
  629. if (!smem->hwlock)
  630. return -ENXIO;
  631. __smem = smem;
  632. return 0;
  633. }
  634. static int qcom_smem_remove(struct platform_device *pdev)
  635. {
  636. hwspin_lock_free(__smem->hwlock);
  637. __smem = NULL;
  638. return 0;
  639. }
  640. static const struct of_device_id qcom_smem_of_match[] = {
  641. { .compatible = "qcom,smem" },
  642. {}
  643. };
  644. MODULE_DEVICE_TABLE(of, qcom_smem_of_match);
  645. static struct platform_driver qcom_smem_driver = {
  646. .probe = qcom_smem_probe,
  647. .remove = qcom_smem_remove,
  648. .driver = {
  649. .name = "qcom-smem",
  650. .of_match_table = qcom_smem_of_match,
  651. .suppress_bind_attrs = true,
  652. },
  653. };
  654. static int __init qcom_smem_init(void)
  655. {
  656. return platform_driver_register(&qcom_smem_driver);
  657. }
  658. arch_initcall(qcom_smem_init);
  659. static void __exit qcom_smem_exit(void)
  660. {
  661. platform_driver_unregister(&qcom_smem_driver);
  662. }
  663. module_exit(qcom_smem_exit)
  664. MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
  665. MODULE_DESCRIPTION("Qualcomm Shared Memory Manager");
  666. MODULE_LICENSE("GPL v2");