smem.c 27 KB

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