smem.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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/sizes.h>
  21. #include <linux/slab.h>
  22. #include <linux/soc/qcom/smem.h>
  23. /*
  24. * The Qualcomm shared memory system is a allocate only heap structure that
  25. * consists of one of more memory areas that can be accessed by the processors
  26. * in the SoC.
  27. *
  28. * All systems contains a global heap, accessible by all processors in the SoC,
  29. * with a table of contents data structure (@smem_header) at the beginning of
  30. * the main shared memory block.
  31. *
  32. * The global header contains meta data for allocations as well as a fixed list
  33. * of 512 entries (@smem_global_entry) that can be initialized to reference
  34. * parts of the shared memory space.
  35. *
  36. *
  37. * In addition to this global heap a set of "private" heaps can be set up at
  38. * boot time with access restrictions so that only certain processor pairs can
  39. * access the data.
  40. *
  41. * These partitions are referenced from an optional partition table
  42. * (@smem_ptable), that is found 4kB from the end of the main smem region. The
  43. * partition table entries (@smem_ptable_entry) lists the involved processors
  44. * (or hosts) and their location in the main shared memory region.
  45. *
  46. * Each partition starts with a header (@smem_partition_header) that identifies
  47. * the partition and holds properties for the two internal memory regions. The
  48. * two regions are cached and non-cached memory respectively. Each region
  49. * contain a link list of allocation headers (@smem_private_entry) followed by
  50. * their data.
  51. *
  52. * Items in the non-cached region are allocated from the start of the partition
  53. * while items in the cached region are allocated from the end. The free area
  54. * is hence the region between the cached and non-cached offsets. The header of
  55. * cached items comes after the data.
  56. *
  57. * Version 12 (SMEM_GLOBAL_PART_VERSION) changes the item alloc/get procedure
  58. * for the global heap. A new global partition is created from the global heap
  59. * region with partition type (SMEM_GLOBAL_HOST) and the max smem item count is
  60. * set by the bootloader.
  61. *
  62. * To synchronize allocations in the shared memory heaps a remote spinlock must
  63. * be held - currently lock number 3 of the sfpb or tcsr is used for this on all
  64. * platforms.
  65. *
  66. */
  67. /*
  68. * The version member of the smem header contains an array of versions for the
  69. * various software components in the SoC. We verify that the boot loader
  70. * version is a valid version as a sanity check.
  71. */
  72. #define SMEM_MASTER_SBL_VERSION_INDEX 7
  73. #define SMEM_GLOBAL_HEAP_VERSION 11
  74. #define SMEM_GLOBAL_PART_VERSION 12
  75. /*
  76. * The first 8 items are only to be allocated by the boot loader while
  77. * initializing the heap.
  78. */
  79. #define SMEM_ITEM_LAST_FIXED 8
  80. /* Highest accepted item number, for both global and private heaps */
  81. #define SMEM_ITEM_COUNT 512
  82. /* Processor/host identifier for the application processor */
  83. #define SMEM_HOST_APPS 0
  84. /* Processor/host identifier for the global partition */
  85. #define SMEM_GLOBAL_HOST 0xfffe
  86. /* Max number of processors/hosts in a system */
  87. #define SMEM_HOST_COUNT 10
  88. /**
  89. * struct smem_proc_comm - proc_comm communication struct (legacy)
  90. * @command: current command to be executed
  91. * @status: status of the currently requested command
  92. * @params: parameters to the command
  93. */
  94. struct smem_proc_comm {
  95. __le32 command;
  96. __le32 status;
  97. __le32 params[2];
  98. };
  99. /**
  100. * struct smem_global_entry - entry to reference smem items on the heap
  101. * @allocated: boolean to indicate if this entry is used
  102. * @offset: offset to the allocated space
  103. * @size: size of the allocated space, 8 byte aligned
  104. * @aux_base: base address for the memory region used by this unit, or 0 for
  105. * the default region. bits 0,1 are reserved
  106. */
  107. struct smem_global_entry {
  108. __le32 allocated;
  109. __le32 offset;
  110. __le32 size;
  111. __le32 aux_base; /* bits 1:0 reserved */
  112. };
  113. #define AUX_BASE_MASK 0xfffffffc
  114. /**
  115. * struct smem_header - header found in beginning of primary smem region
  116. * @proc_comm: proc_comm communication interface (legacy)
  117. * @version: array of versions for the various subsystems
  118. * @initialized: boolean to indicate that smem is initialized
  119. * @free_offset: index of the first unallocated byte in smem
  120. * @available: number of bytes available for allocation
  121. * @reserved: reserved field, must be 0
  122. * toc: array of references to items
  123. */
  124. struct smem_header {
  125. struct smem_proc_comm proc_comm[4];
  126. __le32 version[32];
  127. __le32 initialized;
  128. __le32 free_offset;
  129. __le32 available;
  130. __le32 reserved;
  131. struct smem_global_entry toc[SMEM_ITEM_COUNT];
  132. };
  133. /**
  134. * struct smem_ptable_entry - one entry in the @smem_ptable list
  135. * @offset: offset, within the main shared memory region, of the partition
  136. * @size: size of the partition
  137. * @flags: flags for the partition (currently unused)
  138. * @host0: first processor/host with access to this partition
  139. * @host1: second processor/host with access to this partition
  140. * @cacheline: alignment for "cached" entries
  141. * @reserved: reserved entries for later use
  142. */
  143. struct smem_ptable_entry {
  144. __le32 offset;
  145. __le32 size;
  146. __le32 flags;
  147. __le16 host0;
  148. __le16 host1;
  149. __le32 cacheline;
  150. __le32 reserved[7];
  151. };
  152. /**
  153. * struct smem_ptable - partition table for the private partitions
  154. * @magic: magic number, must be SMEM_PTABLE_MAGIC
  155. * @version: version of the partition table
  156. * @num_entries: number of partitions in the table
  157. * @reserved: for now reserved entries
  158. * @entry: list of @smem_ptable_entry for the @num_entries partitions
  159. */
  160. struct smem_ptable {
  161. u8 magic[4];
  162. __le32 version;
  163. __le32 num_entries;
  164. __le32 reserved[5];
  165. struct smem_ptable_entry entry[];
  166. };
  167. static const u8 SMEM_PTABLE_MAGIC[] = { 0x24, 0x54, 0x4f, 0x43 }; /* "$TOC" */
  168. /**
  169. * struct smem_partition_header - header of the partitions
  170. * @magic: magic number, must be SMEM_PART_MAGIC
  171. * @host0: first processor/host with access to this partition
  172. * @host1: second processor/host with access to this partition
  173. * @size: size of the partition
  174. * @offset_free_uncached: offset to the first free byte of uncached memory in
  175. * this partition
  176. * @offset_free_cached: offset to the first free byte of cached memory in this
  177. * partition
  178. * @reserved: for now reserved entries
  179. */
  180. struct smem_partition_header {
  181. u8 magic[4];
  182. __le16 host0;
  183. __le16 host1;
  184. __le32 size;
  185. __le32 offset_free_uncached;
  186. __le32 offset_free_cached;
  187. __le32 reserved[3];
  188. };
  189. static const u8 SMEM_PART_MAGIC[] = { 0x24, 0x50, 0x52, 0x54 };
  190. /**
  191. * struct smem_private_entry - header of each item in the private partition
  192. * @canary: magic number, must be SMEM_PRIVATE_CANARY
  193. * @item: identifying number of the smem item
  194. * @size: size of the data, including padding bytes
  195. * @padding_data: number of bytes of padding of data
  196. * @padding_hdr: number of bytes of padding between the header and the data
  197. * @reserved: for now reserved entry
  198. */
  199. struct smem_private_entry {
  200. u16 canary; /* bytes are the same so no swapping needed */
  201. __le16 item;
  202. __le32 size; /* includes padding bytes */
  203. __le16 padding_data;
  204. __le16 padding_hdr;
  205. __le32 reserved;
  206. };
  207. #define SMEM_PRIVATE_CANARY 0xa5a5
  208. /**
  209. * struct smem_info - smem region info located after the table of contents
  210. * @magic: magic number, must be SMEM_INFO_MAGIC
  211. * @size: size of the smem region
  212. * @base_addr: base address of the smem region
  213. * @reserved: for now reserved entry
  214. * @num_items: highest accepted item number
  215. */
  216. struct smem_info {
  217. u8 magic[4];
  218. __le32 size;
  219. __le32 base_addr;
  220. __le32 reserved;
  221. __le16 num_items;
  222. };
  223. static const u8 SMEM_INFO_MAGIC[] = { 0x53, 0x49, 0x49, 0x49 }; /* SIII */
  224. /**
  225. * struct smem_region - representation of a chunk of memory used for smem
  226. * @aux_base: identifier of aux_mem base
  227. * @virt_base: virtual base address of memory with this aux_mem identifier
  228. * @size: size of the memory region
  229. */
  230. struct smem_region {
  231. u32 aux_base;
  232. void __iomem *virt_base;
  233. size_t size;
  234. };
  235. /**
  236. * struct qcom_smem - device data for the smem device
  237. * @dev: device pointer
  238. * @hwlock: reference to a hwspinlock
  239. * @global_partition: pointer to global partition when in use
  240. * @global_cacheline: cacheline size for global partition
  241. * @partitions: list of pointers to partitions affecting the current
  242. * processor/host
  243. * @cacheline: list of cacheline sizes for each host
  244. * @item_count: max accepted item number
  245. * @num_regions: number of @regions
  246. * @regions: list of the memory regions defining the shared memory
  247. */
  248. struct qcom_smem {
  249. struct device *dev;
  250. struct hwspinlock *hwlock;
  251. struct smem_partition_header *global_partition;
  252. size_t global_cacheline;
  253. struct smem_partition_header *partitions[SMEM_HOST_COUNT];
  254. size_t cacheline[SMEM_HOST_COUNT];
  255. u32 item_count;
  256. unsigned num_regions;
  257. struct smem_region regions[];
  258. };
  259. static void *
  260. phdr_to_last_uncached_entry(struct smem_partition_header *phdr)
  261. {
  262. void *p = phdr;
  263. return p + le32_to_cpu(phdr->offset_free_uncached);
  264. }
  265. static struct smem_private_entry *
  266. phdr_to_first_cached_entry(struct smem_partition_header *phdr,
  267. size_t cacheline)
  268. {
  269. void *p = phdr;
  270. struct smem_private_entry *e;
  271. return p + le32_to_cpu(phdr->size) - ALIGN(sizeof(*e), cacheline);
  272. }
  273. static void *
  274. phdr_to_last_cached_entry(struct smem_partition_header *phdr)
  275. {
  276. void *p = phdr;
  277. return p + le32_to_cpu(phdr->offset_free_cached);
  278. }
  279. static struct smem_private_entry *
  280. phdr_to_first_uncached_entry(struct smem_partition_header *phdr)
  281. {
  282. void *p = phdr;
  283. return p + sizeof(*phdr);
  284. }
  285. static struct smem_private_entry *
  286. uncached_entry_next(struct smem_private_entry *e)
  287. {
  288. void *p = e;
  289. return p + sizeof(*e) + le16_to_cpu(e->padding_hdr) +
  290. le32_to_cpu(e->size);
  291. }
  292. static struct smem_private_entry *
  293. cached_entry_next(struct smem_private_entry *e, size_t cacheline)
  294. {
  295. void *p = e;
  296. return p - le32_to_cpu(e->size) - ALIGN(sizeof(*e), cacheline);
  297. }
  298. static void *uncached_entry_to_item(struct smem_private_entry *e)
  299. {
  300. void *p = e;
  301. return p + sizeof(*e) + le16_to_cpu(e->padding_hdr);
  302. }
  303. static void *cached_entry_to_item(struct smem_private_entry *e)
  304. {
  305. void *p = e;
  306. return p - le32_to_cpu(e->size);
  307. }
  308. /* Pointer to the one and only smem handle */
  309. static struct qcom_smem *__smem;
  310. /* Timeout (ms) for the trylock of remote spinlocks */
  311. #define HWSPINLOCK_TIMEOUT 1000
  312. static int qcom_smem_alloc_private(struct qcom_smem *smem,
  313. struct smem_partition_header *phdr,
  314. unsigned item,
  315. size_t size)
  316. {
  317. struct smem_private_entry *hdr, *end;
  318. size_t alloc_size;
  319. void *cached;
  320. hdr = phdr_to_first_uncached_entry(phdr);
  321. end = phdr_to_last_uncached_entry(phdr);
  322. cached = phdr_to_last_cached_entry(phdr);
  323. while (hdr < end) {
  324. if (hdr->canary != SMEM_PRIVATE_CANARY)
  325. goto bad_canary;
  326. if (le16_to_cpu(hdr->item) == item)
  327. return -EEXIST;
  328. hdr = uncached_entry_next(hdr);
  329. }
  330. /* Check that we don't grow into the cached region */
  331. alloc_size = sizeof(*hdr) + ALIGN(size, 8);
  332. if ((void *)hdr + alloc_size > cached) {
  333. dev_err(smem->dev, "Out of memory\n");
  334. return -ENOSPC;
  335. }
  336. hdr->canary = SMEM_PRIVATE_CANARY;
  337. hdr->item = cpu_to_le16(item);
  338. hdr->size = cpu_to_le32(ALIGN(size, 8));
  339. hdr->padding_data = cpu_to_le16(le32_to_cpu(hdr->size) - size);
  340. hdr->padding_hdr = 0;
  341. /*
  342. * Ensure the header is written before we advance the free offset, so
  343. * that remote processors that does not take the remote spinlock still
  344. * gets a consistent view of the linked list.
  345. */
  346. wmb();
  347. le32_add_cpu(&phdr->offset_free_uncached, alloc_size);
  348. return 0;
  349. bad_canary:
  350. dev_err(smem->dev, "Found invalid canary in hosts %hu:%hu partition\n",
  351. le16_to_cpu(phdr->host0), le16_to_cpu(phdr->host1));
  352. return -EINVAL;
  353. }
  354. static int qcom_smem_alloc_global(struct qcom_smem *smem,
  355. unsigned item,
  356. size_t size)
  357. {
  358. struct smem_global_entry *entry;
  359. struct smem_header *header;
  360. header = smem->regions[0].virt_base;
  361. entry = &header->toc[item];
  362. if (entry->allocated)
  363. return -EEXIST;
  364. size = ALIGN(size, 8);
  365. if (WARN_ON(size > le32_to_cpu(header->available)))
  366. return -ENOMEM;
  367. entry->offset = header->free_offset;
  368. entry->size = cpu_to_le32(size);
  369. /*
  370. * Ensure the header is consistent before we mark the item allocated,
  371. * so that remote processors will get a consistent view of the item
  372. * even though they do not take the spinlock on read.
  373. */
  374. wmb();
  375. entry->allocated = cpu_to_le32(1);
  376. le32_add_cpu(&header->free_offset, size);
  377. le32_add_cpu(&header->available, -size);
  378. return 0;
  379. }
  380. /**
  381. * qcom_smem_alloc() - allocate space for a smem item
  382. * @host: remote processor id, or -1
  383. * @item: smem item handle
  384. * @size: number of bytes to be allocated
  385. *
  386. * Allocate space for a given smem item of size @size, given that the item is
  387. * not yet allocated.
  388. */
  389. int qcom_smem_alloc(unsigned host, unsigned item, size_t size)
  390. {
  391. struct smem_partition_header *phdr;
  392. unsigned long flags;
  393. int ret;
  394. if (!__smem)
  395. return -EPROBE_DEFER;
  396. if (item < SMEM_ITEM_LAST_FIXED) {
  397. dev_err(__smem->dev,
  398. "Rejecting allocation of static entry %d\n", item);
  399. return -EINVAL;
  400. }
  401. if (WARN_ON(item >= __smem->item_count))
  402. return -EINVAL;
  403. ret = hwspin_lock_timeout_irqsave(__smem->hwlock,
  404. HWSPINLOCK_TIMEOUT,
  405. &flags);
  406. if (ret)
  407. return ret;
  408. if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
  409. phdr = __smem->partitions[host];
  410. ret = qcom_smem_alloc_private(__smem, phdr, item, size);
  411. } else if (__smem->global_partition) {
  412. phdr = __smem->global_partition;
  413. ret = qcom_smem_alloc_private(__smem, phdr, item, size);
  414. } else {
  415. ret = qcom_smem_alloc_global(__smem, item, size);
  416. }
  417. hwspin_unlock_irqrestore(__smem->hwlock, &flags);
  418. return ret;
  419. }
  420. EXPORT_SYMBOL(qcom_smem_alloc);
  421. static void *qcom_smem_get_global(struct qcom_smem *smem,
  422. unsigned item,
  423. size_t *size)
  424. {
  425. struct smem_header *header;
  426. struct smem_region *region;
  427. struct smem_global_entry *entry;
  428. u32 aux_base;
  429. unsigned i;
  430. header = smem->regions[0].virt_base;
  431. entry = &header->toc[item];
  432. if (!entry->allocated)
  433. return ERR_PTR(-ENXIO);
  434. aux_base = le32_to_cpu(entry->aux_base) & AUX_BASE_MASK;
  435. for (i = 0; i < smem->num_regions; i++) {
  436. region = &smem->regions[i];
  437. if (region->aux_base == aux_base || !aux_base) {
  438. if (size != NULL)
  439. *size = le32_to_cpu(entry->size);
  440. return region->virt_base + le32_to_cpu(entry->offset);
  441. }
  442. }
  443. return ERR_PTR(-ENOENT);
  444. }
  445. static void *qcom_smem_get_private(struct qcom_smem *smem,
  446. struct smem_partition_header *phdr,
  447. size_t cacheline,
  448. unsigned item,
  449. size_t *size)
  450. {
  451. struct smem_private_entry *e, *end;
  452. e = phdr_to_first_uncached_entry(phdr);
  453. end = phdr_to_last_uncached_entry(phdr);
  454. while (e < end) {
  455. if (e->canary != SMEM_PRIVATE_CANARY)
  456. goto invalid_canary;
  457. if (le16_to_cpu(e->item) == item) {
  458. if (size != NULL)
  459. *size = le32_to_cpu(e->size) -
  460. le16_to_cpu(e->padding_data);
  461. return uncached_entry_to_item(e);
  462. }
  463. e = uncached_entry_next(e);
  464. }
  465. /* Item was not found in the uncached list, search the cached list */
  466. e = phdr_to_first_cached_entry(phdr, cacheline);
  467. end = phdr_to_last_cached_entry(phdr);
  468. while (e > end) {
  469. if (e->canary != SMEM_PRIVATE_CANARY)
  470. goto invalid_canary;
  471. if (le16_to_cpu(e->item) == item) {
  472. if (size != NULL)
  473. *size = le32_to_cpu(e->size) -
  474. le16_to_cpu(e->padding_data);
  475. return cached_entry_to_item(e);
  476. }
  477. e = cached_entry_next(e, cacheline);
  478. }
  479. return ERR_PTR(-ENOENT);
  480. invalid_canary:
  481. dev_err(smem->dev, "Found invalid canary in hosts %hu:%hu partition\n",
  482. le16_to_cpu(phdr->host0), le16_to_cpu(phdr->host1));
  483. return ERR_PTR(-EINVAL);
  484. }
  485. /**
  486. * qcom_smem_get() - resolve ptr of size of a smem item
  487. * @host: the remote processor, or -1
  488. * @item: smem item handle
  489. * @size: pointer to be filled out with size of the item
  490. *
  491. * Looks up smem item and returns pointer to it. Size of smem
  492. * item is returned in @size.
  493. */
  494. void *qcom_smem_get(unsigned host, unsigned item, size_t *size)
  495. {
  496. struct smem_partition_header *phdr;
  497. unsigned long flags;
  498. size_t cacheln;
  499. int ret;
  500. void *ptr = ERR_PTR(-EPROBE_DEFER);
  501. if (!__smem)
  502. return ptr;
  503. if (WARN_ON(item >= __smem->item_count))
  504. return ERR_PTR(-EINVAL);
  505. ret = hwspin_lock_timeout_irqsave(__smem->hwlock,
  506. HWSPINLOCK_TIMEOUT,
  507. &flags);
  508. if (ret)
  509. return ERR_PTR(ret);
  510. if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
  511. phdr = __smem->partitions[host];
  512. cacheln = __smem->cacheline[host];
  513. ptr = qcom_smem_get_private(__smem, phdr, cacheln, item, size);
  514. } else if (__smem->global_partition) {
  515. phdr = __smem->global_partition;
  516. cacheln = __smem->global_cacheline;
  517. ptr = qcom_smem_get_private(__smem, phdr, cacheln, item, size);
  518. } else {
  519. ptr = qcom_smem_get_global(__smem, item, size);
  520. }
  521. hwspin_unlock_irqrestore(__smem->hwlock, &flags);
  522. return ptr;
  523. }
  524. EXPORT_SYMBOL(qcom_smem_get);
  525. /**
  526. * qcom_smem_get_free_space() - retrieve amount of free space in a partition
  527. * @host: the remote processor identifying a partition, or -1
  528. *
  529. * To be used by smem clients as a quick way to determine if any new
  530. * allocations has been made.
  531. */
  532. int qcom_smem_get_free_space(unsigned host)
  533. {
  534. struct smem_partition_header *phdr;
  535. struct smem_header *header;
  536. unsigned ret;
  537. if (!__smem)
  538. return -EPROBE_DEFER;
  539. if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
  540. phdr = __smem->partitions[host];
  541. ret = le32_to_cpu(phdr->offset_free_cached) -
  542. le32_to_cpu(phdr->offset_free_uncached);
  543. } else if (__smem->global_partition) {
  544. phdr = __smem->global_partition;
  545. ret = le32_to_cpu(phdr->offset_free_cached) -
  546. le32_to_cpu(phdr->offset_free_uncached);
  547. } else {
  548. header = __smem->regions[0].virt_base;
  549. ret = le32_to_cpu(header->available);
  550. }
  551. return ret;
  552. }
  553. EXPORT_SYMBOL(qcom_smem_get_free_space);
  554. /**
  555. * qcom_smem_virt_to_phys() - return the physical address associated
  556. * with an smem item pointer (previously returned by qcom_smem_get()
  557. * @p: the virtual address to convert
  558. *
  559. * Returns 0 if the pointer provided is not within any smem region.
  560. */
  561. phys_addr_t qcom_smem_virt_to_phys(void *p)
  562. {
  563. unsigned i;
  564. for (i = 0; i < __smem->num_regions; i++) {
  565. struct smem_region *region = &__smem->regions[i];
  566. if (p < region->virt_base)
  567. continue;
  568. if (p < region->virt_base + region->size) {
  569. u64 offset = p - region->virt_base;
  570. return (phys_addr_t)region->aux_base + offset;
  571. }
  572. }
  573. return 0;
  574. }
  575. EXPORT_SYMBOL(qcom_smem_virt_to_phys);
  576. static int qcom_smem_get_sbl_version(struct qcom_smem *smem)
  577. {
  578. struct smem_header *header;
  579. __le32 *versions;
  580. header = smem->regions[0].virt_base;
  581. versions = header->version;
  582. return le32_to_cpu(versions[SMEM_MASTER_SBL_VERSION_INDEX]);
  583. }
  584. static struct smem_ptable *qcom_smem_get_ptable(struct qcom_smem *smem)
  585. {
  586. struct smem_ptable *ptable;
  587. u32 version;
  588. ptable = smem->regions[0].virt_base + smem->regions[0].size - SZ_4K;
  589. if (memcmp(ptable->magic, SMEM_PTABLE_MAGIC, sizeof(ptable->magic)))
  590. return ERR_PTR(-ENOENT);
  591. version = le32_to_cpu(ptable->version);
  592. if (version != 1) {
  593. dev_err(smem->dev,
  594. "Unsupported partition header version %d\n", version);
  595. return ERR_PTR(-EINVAL);
  596. }
  597. return ptable;
  598. }
  599. static u32 qcom_smem_get_item_count(struct qcom_smem *smem)
  600. {
  601. struct smem_ptable *ptable;
  602. struct smem_info *info;
  603. ptable = qcom_smem_get_ptable(smem);
  604. if (IS_ERR_OR_NULL(ptable))
  605. return SMEM_ITEM_COUNT;
  606. info = (struct smem_info *)&ptable->entry[ptable->num_entries];
  607. if (memcmp(info->magic, SMEM_INFO_MAGIC, sizeof(info->magic)))
  608. return SMEM_ITEM_COUNT;
  609. return le16_to_cpu(info->num_items);
  610. }
  611. /*
  612. * Validate the partition header for a partition whose partition
  613. * table entry is supplied. Returns a pointer to its header if
  614. * valid, or a null pointer otherwise.
  615. */
  616. static struct smem_partition_header *
  617. qcom_smem_partition_header(struct qcom_smem *smem,
  618. struct smem_ptable_entry *entry, u16 host0, u16 host1)
  619. {
  620. struct smem_partition_header *header;
  621. u32 size;
  622. header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
  623. if (memcmp(header->magic, SMEM_PART_MAGIC, sizeof(header->magic))) {
  624. dev_err(smem->dev, "bad partition magic %02x %02x %02x %02x\n",
  625. header->magic[0], header->magic[1],
  626. header->magic[2], header->magic[3]);
  627. return NULL;
  628. }
  629. if (host0 != le16_to_cpu(header->host0)) {
  630. dev_err(smem->dev, "bad host0 (%hu != %hu)\n",
  631. host0, le16_to_cpu(header->host0));
  632. return NULL;
  633. }
  634. if (host1 != le16_to_cpu(header->host1)) {
  635. dev_err(smem->dev, "bad host1 (%hu != %hu)\n",
  636. host1, le16_to_cpu(header->host1));
  637. return NULL;
  638. }
  639. size = le32_to_cpu(header->size);
  640. if (size != le32_to_cpu(entry->size)) {
  641. dev_err(smem->dev, "bad partition size (%u != %u)\n",
  642. size, le32_to_cpu(entry->size));
  643. return NULL;
  644. }
  645. if (le32_to_cpu(header->offset_free_uncached) > size) {
  646. dev_err(smem->dev, "bad partition free uncached (%u > %u)\n",
  647. le32_to_cpu(header->offset_free_uncached), size);
  648. return NULL;
  649. }
  650. return header;
  651. }
  652. static int qcom_smem_set_global_partition(struct qcom_smem *smem)
  653. {
  654. struct smem_partition_header *header;
  655. struct smem_ptable_entry *entry;
  656. struct smem_ptable *ptable;
  657. bool found = false;
  658. int i;
  659. if (smem->global_partition) {
  660. dev_err(smem->dev, "Already found the global partition\n");
  661. return -EINVAL;
  662. }
  663. ptable = qcom_smem_get_ptable(smem);
  664. if (IS_ERR(ptable))
  665. return PTR_ERR(ptable);
  666. for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
  667. entry = &ptable->entry[i];
  668. if (!le32_to_cpu(entry->offset))
  669. continue;
  670. if (!le32_to_cpu(entry->size))
  671. continue;
  672. if (le16_to_cpu(entry->host0) != SMEM_GLOBAL_HOST)
  673. continue;
  674. if (le16_to_cpu(entry->host1) == SMEM_GLOBAL_HOST) {
  675. found = true;
  676. break;
  677. }
  678. }
  679. if (!found) {
  680. dev_err(smem->dev, "Missing entry for global partition\n");
  681. return -EINVAL;
  682. }
  683. header = qcom_smem_partition_header(smem, entry,
  684. SMEM_GLOBAL_HOST, SMEM_GLOBAL_HOST);
  685. if (!header)
  686. return -EINVAL;
  687. smem->global_partition = header;
  688. smem->global_cacheline = le32_to_cpu(entry->cacheline);
  689. return 0;
  690. }
  691. static int
  692. qcom_smem_enumerate_partitions(struct qcom_smem *smem, u16 local_host)
  693. {
  694. struct smem_partition_header *header;
  695. struct smem_ptable_entry *entry;
  696. struct smem_ptable *ptable;
  697. unsigned int remote_host;
  698. u16 host0, host1;
  699. int i;
  700. ptable = qcom_smem_get_ptable(smem);
  701. if (IS_ERR(ptable))
  702. return PTR_ERR(ptable);
  703. for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
  704. entry = &ptable->entry[i];
  705. if (!le32_to_cpu(entry->offset))
  706. continue;
  707. if (!le32_to_cpu(entry->size))
  708. continue;
  709. host0 = le16_to_cpu(entry->host0);
  710. host1 = le16_to_cpu(entry->host1);
  711. if (host0 == local_host)
  712. remote_host = host1;
  713. else if (host1 == local_host)
  714. remote_host = host0;
  715. else
  716. continue;
  717. if (remote_host >= SMEM_HOST_COUNT) {
  718. dev_err(smem->dev, "bad host %hu\n", remote_host);
  719. return -EINVAL;
  720. }
  721. if (smem->partitions[remote_host]) {
  722. dev_err(smem->dev, "duplicate host %hu\n", remote_host);
  723. return -EINVAL;
  724. }
  725. header = qcom_smem_partition_header(smem, entry, host0, host1);
  726. if (!header)
  727. return -EINVAL;
  728. smem->partitions[remote_host] = header;
  729. smem->cacheline[remote_host] = le32_to_cpu(entry->cacheline);
  730. }
  731. return 0;
  732. }
  733. static int qcom_smem_map_memory(struct qcom_smem *smem, struct device *dev,
  734. const char *name, int i)
  735. {
  736. struct device_node *np;
  737. struct resource r;
  738. resource_size_t size;
  739. int ret;
  740. np = of_parse_phandle(dev->of_node, name, 0);
  741. if (!np) {
  742. dev_err(dev, "No %s specified\n", name);
  743. return -EINVAL;
  744. }
  745. ret = of_address_to_resource(np, 0, &r);
  746. of_node_put(np);
  747. if (ret)
  748. return ret;
  749. size = resource_size(&r);
  750. smem->regions[i].virt_base = devm_ioremap_wc(dev, r.start, size);
  751. if (!smem->regions[i].virt_base)
  752. return -ENOMEM;
  753. smem->regions[i].aux_base = (u32)r.start;
  754. smem->regions[i].size = size;
  755. return 0;
  756. }
  757. static int qcom_smem_probe(struct platform_device *pdev)
  758. {
  759. struct smem_header *header;
  760. struct qcom_smem *smem;
  761. size_t array_size;
  762. int num_regions;
  763. int hwlock_id;
  764. u32 version;
  765. int ret;
  766. num_regions = 1;
  767. if (of_find_property(pdev->dev.of_node, "qcom,rpm-msg-ram", NULL))
  768. num_regions++;
  769. array_size = num_regions * sizeof(struct smem_region);
  770. smem = devm_kzalloc(&pdev->dev, sizeof(*smem) + array_size, GFP_KERNEL);
  771. if (!smem)
  772. return -ENOMEM;
  773. smem->dev = &pdev->dev;
  774. smem->num_regions = num_regions;
  775. ret = qcom_smem_map_memory(smem, &pdev->dev, "memory-region", 0);
  776. if (ret)
  777. return ret;
  778. if (num_regions > 1 && (ret = qcom_smem_map_memory(smem, &pdev->dev,
  779. "qcom,rpm-msg-ram", 1)))
  780. return ret;
  781. header = smem->regions[0].virt_base;
  782. if (le32_to_cpu(header->initialized) != 1 ||
  783. le32_to_cpu(header->reserved)) {
  784. dev_err(&pdev->dev, "SMEM is not initialized by SBL\n");
  785. return -EINVAL;
  786. }
  787. version = qcom_smem_get_sbl_version(smem);
  788. switch (version >> 16) {
  789. case SMEM_GLOBAL_PART_VERSION:
  790. ret = qcom_smem_set_global_partition(smem);
  791. if (ret < 0)
  792. return ret;
  793. smem->item_count = qcom_smem_get_item_count(smem);
  794. break;
  795. case SMEM_GLOBAL_HEAP_VERSION:
  796. smem->item_count = SMEM_ITEM_COUNT;
  797. break;
  798. default:
  799. dev_err(&pdev->dev, "Unsupported SMEM version 0x%x\n", version);
  800. return -EINVAL;
  801. }
  802. BUILD_BUG_ON(SMEM_HOST_APPS >= SMEM_HOST_COUNT);
  803. ret = qcom_smem_enumerate_partitions(smem, SMEM_HOST_APPS);
  804. if (ret < 0 && ret != -ENOENT)
  805. return ret;
  806. hwlock_id = of_hwspin_lock_get_id(pdev->dev.of_node, 0);
  807. if (hwlock_id < 0) {
  808. if (hwlock_id != -EPROBE_DEFER)
  809. dev_err(&pdev->dev, "failed to retrieve hwlock\n");
  810. return hwlock_id;
  811. }
  812. smem->hwlock = hwspin_lock_request_specific(hwlock_id);
  813. if (!smem->hwlock)
  814. return -ENXIO;
  815. __smem = smem;
  816. return 0;
  817. }
  818. static int qcom_smem_remove(struct platform_device *pdev)
  819. {
  820. hwspin_lock_free(__smem->hwlock);
  821. __smem = NULL;
  822. return 0;
  823. }
  824. static const struct of_device_id qcom_smem_of_match[] = {
  825. { .compatible = "qcom,smem" },
  826. {}
  827. };
  828. MODULE_DEVICE_TABLE(of, qcom_smem_of_match);
  829. static struct platform_driver qcom_smem_driver = {
  830. .probe = qcom_smem_probe,
  831. .remove = qcom_smem_remove,
  832. .driver = {
  833. .name = "qcom-smem",
  834. .of_match_table = qcom_smem_of_match,
  835. .suppress_bind_attrs = true,
  836. },
  837. };
  838. static int __init qcom_smem_init(void)
  839. {
  840. return platform_driver_register(&qcom_smem_driver);
  841. }
  842. arch_initcall(qcom_smem_init);
  843. static void __exit qcom_smem_exit(void)
  844. {
  845. platform_driver_unregister(&qcom_smem_driver);
  846. }
  847. module_exit(qcom_smem_exit)
  848. MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
  849. MODULE_DESCRIPTION("Qualcomm Shared Memory Manager");
  850. MODULE_LICENSE("GPL v2");