smem.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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 struct smem_private_entry *
  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 void *phdr_to_first_cached_entry(struct smem_partition_header *phdr,
  265. size_t cacheline)
  266. {
  267. void *p = phdr;
  268. return p + le32_to_cpu(phdr->size) - ALIGN(sizeof(*phdr), cacheline);
  269. }
  270. static void *phdr_to_last_cached_entry(struct smem_partition_header *phdr)
  271. {
  272. void *p = phdr;
  273. return p + le32_to_cpu(phdr->offset_free_cached);
  274. }
  275. static struct smem_private_entry *
  276. phdr_to_first_uncached_entry(struct smem_partition_header *phdr)
  277. {
  278. void *p = phdr;
  279. return p + sizeof(*phdr);
  280. }
  281. static struct smem_private_entry *
  282. uncached_entry_next(struct smem_private_entry *e)
  283. {
  284. void *p = e;
  285. return p + sizeof(*e) + le16_to_cpu(e->padding_hdr) +
  286. le32_to_cpu(e->size);
  287. }
  288. static struct smem_private_entry *
  289. cached_entry_next(struct smem_private_entry *e, size_t cacheline)
  290. {
  291. void *p = e;
  292. return p - le32_to_cpu(e->size) - ALIGN(sizeof(*e), cacheline);
  293. }
  294. static void *uncached_entry_to_item(struct smem_private_entry *e)
  295. {
  296. void *p = e;
  297. return p + sizeof(*e) + le16_to_cpu(e->padding_hdr);
  298. }
  299. static void *cached_entry_to_item(struct smem_private_entry *e)
  300. {
  301. void *p = e;
  302. return p - le32_to_cpu(e->size);
  303. }
  304. /* Pointer to the one and only smem handle */
  305. static struct qcom_smem *__smem;
  306. /* Timeout (ms) for the trylock of remote spinlocks */
  307. #define HWSPINLOCK_TIMEOUT 1000
  308. static int qcom_smem_alloc_private(struct qcom_smem *smem,
  309. struct smem_partition_header *phdr,
  310. unsigned item,
  311. size_t size)
  312. {
  313. struct smem_private_entry *hdr, *end;
  314. size_t alloc_size;
  315. void *cached;
  316. hdr = phdr_to_first_uncached_entry(phdr);
  317. end = phdr_to_last_uncached_entry(phdr);
  318. cached = phdr_to_last_cached_entry(phdr);
  319. while (hdr < end) {
  320. if (hdr->canary != SMEM_PRIVATE_CANARY) {
  321. dev_err(smem->dev,
  322. "Found invalid canary in hosts %d:%d partition\n",
  323. phdr->host0, phdr->host1);
  324. return -EINVAL;
  325. }
  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. }
  350. static int qcom_smem_alloc_global(struct qcom_smem *smem,
  351. unsigned item,
  352. size_t size)
  353. {
  354. struct smem_global_entry *entry;
  355. struct smem_header *header;
  356. header = smem->regions[0].virt_base;
  357. entry = &header->toc[item];
  358. if (entry->allocated)
  359. return -EEXIST;
  360. size = ALIGN(size, 8);
  361. if (WARN_ON(size > le32_to_cpu(header->available)))
  362. return -ENOMEM;
  363. entry->offset = header->free_offset;
  364. entry->size = cpu_to_le32(size);
  365. /*
  366. * Ensure the header is consistent before we mark the item allocated,
  367. * so that remote processors will get a consistent view of the item
  368. * even though they do not take the spinlock on read.
  369. */
  370. wmb();
  371. entry->allocated = cpu_to_le32(1);
  372. le32_add_cpu(&header->free_offset, size);
  373. le32_add_cpu(&header->available, -size);
  374. return 0;
  375. }
  376. /**
  377. * qcom_smem_alloc() - allocate space for a smem item
  378. * @host: remote processor id, or -1
  379. * @item: smem item handle
  380. * @size: number of bytes to be allocated
  381. *
  382. * Allocate space for a given smem item of size @size, given that the item is
  383. * not yet allocated.
  384. */
  385. int qcom_smem_alloc(unsigned host, unsigned item, size_t size)
  386. {
  387. struct smem_partition_header *phdr;
  388. unsigned long flags;
  389. int ret;
  390. if (!__smem)
  391. return -EPROBE_DEFER;
  392. if (item < SMEM_ITEM_LAST_FIXED) {
  393. dev_err(__smem->dev,
  394. "Rejecting allocation of static entry %d\n", item);
  395. return -EINVAL;
  396. }
  397. if (WARN_ON(item >= __smem->item_count))
  398. return -EINVAL;
  399. ret = hwspin_lock_timeout_irqsave(__smem->hwlock,
  400. HWSPINLOCK_TIMEOUT,
  401. &flags);
  402. if (ret)
  403. return ret;
  404. if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
  405. phdr = __smem->partitions[host];
  406. ret = qcom_smem_alloc_private(__smem, phdr, item, size);
  407. } else if (__smem->global_partition) {
  408. phdr = __smem->global_partition;
  409. ret = qcom_smem_alloc_private(__smem, phdr, item, size);
  410. } else {
  411. ret = qcom_smem_alloc_global(__smem, item, size);
  412. }
  413. hwspin_unlock_irqrestore(__smem->hwlock, &flags);
  414. return ret;
  415. }
  416. EXPORT_SYMBOL(qcom_smem_alloc);
  417. static void *qcom_smem_get_global(struct qcom_smem *smem,
  418. unsigned item,
  419. size_t *size)
  420. {
  421. struct smem_header *header;
  422. struct smem_region *area;
  423. struct smem_global_entry *entry;
  424. u32 aux_base;
  425. unsigned i;
  426. header = smem->regions[0].virt_base;
  427. entry = &header->toc[item];
  428. if (!entry->allocated)
  429. return ERR_PTR(-ENXIO);
  430. aux_base = le32_to_cpu(entry->aux_base) & AUX_BASE_MASK;
  431. for (i = 0; i < smem->num_regions; i++) {
  432. area = &smem->regions[i];
  433. if (area->aux_base == aux_base || !aux_base) {
  434. if (size != NULL)
  435. *size = le32_to_cpu(entry->size);
  436. return area->virt_base + le32_to_cpu(entry->offset);
  437. }
  438. }
  439. return ERR_PTR(-ENOENT);
  440. }
  441. static void *qcom_smem_get_private(struct qcom_smem *smem,
  442. struct smem_partition_header *phdr,
  443. size_t cacheline,
  444. unsigned item,
  445. size_t *size)
  446. {
  447. struct smem_private_entry *e, *end;
  448. e = phdr_to_first_uncached_entry(phdr);
  449. end = phdr_to_last_uncached_entry(phdr);
  450. while (e < end) {
  451. if (e->canary != SMEM_PRIVATE_CANARY)
  452. goto invalid_canary;
  453. if (le16_to_cpu(e->item) == item) {
  454. if (size != NULL)
  455. *size = le32_to_cpu(e->size) -
  456. le16_to_cpu(e->padding_data);
  457. return uncached_entry_to_item(e);
  458. }
  459. e = uncached_entry_next(e);
  460. }
  461. /* Item was not found in the uncached list, search the cached list */
  462. e = phdr_to_first_cached_entry(phdr, cacheline);
  463. end = phdr_to_last_cached_entry(phdr);
  464. while (e > end) {
  465. if (e->canary != SMEM_PRIVATE_CANARY)
  466. goto invalid_canary;
  467. if (le16_to_cpu(e->item) == item) {
  468. if (size != NULL)
  469. *size = le32_to_cpu(e->size) -
  470. le16_to_cpu(e->padding_data);
  471. return cached_entry_to_item(e);
  472. }
  473. e = cached_entry_next(e, cacheline);
  474. }
  475. return ERR_PTR(-ENOENT);
  476. invalid_canary:
  477. dev_err(smem->dev, "Found invalid canary in hosts %d:%d partition\n",
  478. phdr->host0, phdr->host1);
  479. return ERR_PTR(-EINVAL);
  480. }
  481. /**
  482. * qcom_smem_get() - resolve ptr of size of a smem item
  483. * @host: the remote processor, or -1
  484. * @item: smem item handle
  485. * @size: pointer to be filled out with size of the item
  486. *
  487. * Looks up smem item and returns pointer to it. Size of smem
  488. * item is returned in @size.
  489. */
  490. void *qcom_smem_get(unsigned host, unsigned item, size_t *size)
  491. {
  492. struct smem_partition_header *phdr;
  493. unsigned long flags;
  494. size_t cacheln;
  495. int ret;
  496. void *ptr = ERR_PTR(-EPROBE_DEFER);
  497. if (!__smem)
  498. return ptr;
  499. if (WARN_ON(item >= __smem->item_count))
  500. return ERR_PTR(-EINVAL);
  501. ret = hwspin_lock_timeout_irqsave(__smem->hwlock,
  502. HWSPINLOCK_TIMEOUT,
  503. &flags);
  504. if (ret)
  505. return ERR_PTR(ret);
  506. if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
  507. phdr = __smem->partitions[host];
  508. cacheln = __smem->cacheline[host];
  509. ptr = qcom_smem_get_private(__smem, phdr, cacheln, item, size);
  510. } else if (__smem->global_partition) {
  511. phdr = __smem->global_partition;
  512. cacheln = __smem->global_cacheline;
  513. ptr = qcom_smem_get_private(__smem, phdr, cacheln, item, size);
  514. } else {
  515. ptr = qcom_smem_get_global(__smem, item, size);
  516. }
  517. hwspin_unlock_irqrestore(__smem->hwlock, &flags);
  518. return ptr;
  519. }
  520. EXPORT_SYMBOL(qcom_smem_get);
  521. /**
  522. * qcom_smem_get_free_space() - retrieve amount of free space in a partition
  523. * @host: the remote processor identifying a partition, or -1
  524. *
  525. * To be used by smem clients as a quick way to determine if any new
  526. * allocations has been made.
  527. */
  528. int qcom_smem_get_free_space(unsigned host)
  529. {
  530. struct smem_partition_header *phdr;
  531. struct smem_header *header;
  532. unsigned ret;
  533. if (!__smem)
  534. return -EPROBE_DEFER;
  535. if (host < SMEM_HOST_COUNT && __smem->partitions[host]) {
  536. phdr = __smem->partitions[host];
  537. ret = le32_to_cpu(phdr->offset_free_cached) -
  538. le32_to_cpu(phdr->offset_free_uncached);
  539. } else if (__smem->global_partition) {
  540. phdr = __smem->global_partition;
  541. ret = le32_to_cpu(phdr->offset_free_cached) -
  542. le32_to_cpu(phdr->offset_free_uncached);
  543. } else {
  544. header = __smem->regions[0].virt_base;
  545. ret = le32_to_cpu(header->available);
  546. }
  547. return ret;
  548. }
  549. EXPORT_SYMBOL(qcom_smem_get_free_space);
  550. static int qcom_smem_get_sbl_version(struct qcom_smem *smem)
  551. {
  552. struct smem_header *header;
  553. __le32 *versions;
  554. header = smem->regions[0].virt_base;
  555. versions = header->version;
  556. return le32_to_cpu(versions[SMEM_MASTER_SBL_VERSION_INDEX]);
  557. }
  558. static struct smem_ptable *qcom_smem_get_ptable(struct qcom_smem *smem)
  559. {
  560. struct smem_ptable *ptable;
  561. u32 version;
  562. ptable = smem->regions[0].virt_base + smem->regions[0].size - SZ_4K;
  563. if (memcmp(ptable->magic, SMEM_PTABLE_MAGIC, sizeof(ptable->magic)))
  564. return ERR_PTR(-ENOENT);
  565. version = le32_to_cpu(ptable->version);
  566. if (version != 1) {
  567. dev_err(smem->dev,
  568. "Unsupported partition header version %d\n", version);
  569. return ERR_PTR(-EINVAL);
  570. }
  571. return ptable;
  572. }
  573. static u32 qcom_smem_get_item_count(struct qcom_smem *smem)
  574. {
  575. struct smem_ptable *ptable;
  576. struct smem_info *info;
  577. ptable = qcom_smem_get_ptable(smem);
  578. if (IS_ERR_OR_NULL(ptable))
  579. return SMEM_ITEM_COUNT;
  580. info = (struct smem_info *)&ptable->entry[ptable->num_entries];
  581. if (memcmp(info->magic, SMEM_INFO_MAGIC, sizeof(info->magic)))
  582. return SMEM_ITEM_COUNT;
  583. return le16_to_cpu(info->num_items);
  584. }
  585. static int qcom_smem_set_global_partition(struct qcom_smem *smem)
  586. {
  587. struct smem_partition_header *header;
  588. struct smem_ptable_entry *entry = NULL;
  589. struct smem_ptable *ptable;
  590. u32 host0, host1, size;
  591. int i;
  592. ptable = qcom_smem_get_ptable(smem);
  593. if (IS_ERR(ptable))
  594. return PTR_ERR(ptable);
  595. for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
  596. entry = &ptable->entry[i];
  597. host0 = le16_to_cpu(entry->host0);
  598. host1 = le16_to_cpu(entry->host1);
  599. if (host0 == SMEM_GLOBAL_HOST && host0 == host1)
  600. break;
  601. }
  602. if (!entry) {
  603. dev_err(smem->dev, "Missing entry for global partition\n");
  604. return -EINVAL;
  605. }
  606. if (!le32_to_cpu(entry->offset) || !le32_to_cpu(entry->size)) {
  607. dev_err(smem->dev, "Invalid entry for global partition\n");
  608. return -EINVAL;
  609. }
  610. if (smem->global_partition) {
  611. dev_err(smem->dev, "Already found the global partition\n");
  612. return -EINVAL;
  613. }
  614. header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
  615. host0 = le16_to_cpu(header->host0);
  616. host1 = le16_to_cpu(header->host1);
  617. if (memcmp(header->magic, SMEM_PART_MAGIC, sizeof(header->magic))) {
  618. dev_err(smem->dev, "Global partition has invalid magic\n");
  619. return -EINVAL;
  620. }
  621. if (host0 != SMEM_GLOBAL_HOST && host1 != SMEM_GLOBAL_HOST) {
  622. dev_err(smem->dev, "Global partition hosts are invalid\n");
  623. return -EINVAL;
  624. }
  625. if (le32_to_cpu(header->size) != le32_to_cpu(entry->size)) {
  626. dev_err(smem->dev, "Global partition has invalid size\n");
  627. return -EINVAL;
  628. }
  629. size = le32_to_cpu(header->offset_free_uncached);
  630. if (size > le32_to_cpu(header->size)) {
  631. dev_err(smem->dev,
  632. "Global partition has invalid free pointer\n");
  633. return -EINVAL;
  634. }
  635. smem->global_partition = header;
  636. smem->global_cacheline = le32_to_cpu(entry->cacheline);
  637. return 0;
  638. }
  639. static int qcom_smem_enumerate_partitions(struct qcom_smem *smem,
  640. unsigned int local_host)
  641. {
  642. struct smem_partition_header *header;
  643. struct smem_ptable_entry *entry;
  644. struct smem_ptable *ptable;
  645. unsigned int remote_host;
  646. u32 host0, host1;
  647. int i;
  648. ptable = qcom_smem_get_ptable(smem);
  649. if (IS_ERR(ptable))
  650. return PTR_ERR(ptable);
  651. for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
  652. entry = &ptable->entry[i];
  653. host0 = le16_to_cpu(entry->host0);
  654. host1 = le16_to_cpu(entry->host1);
  655. if (host0 != local_host && host1 != local_host)
  656. continue;
  657. if (!le32_to_cpu(entry->offset))
  658. continue;
  659. if (!le32_to_cpu(entry->size))
  660. continue;
  661. if (host0 == local_host)
  662. remote_host = host1;
  663. else
  664. remote_host = host0;
  665. if (remote_host >= SMEM_HOST_COUNT) {
  666. dev_err(smem->dev,
  667. "Invalid remote host %d\n",
  668. remote_host);
  669. return -EINVAL;
  670. }
  671. if (smem->partitions[remote_host]) {
  672. dev_err(smem->dev,
  673. "Already found a partition for host %d\n",
  674. remote_host);
  675. return -EINVAL;
  676. }
  677. header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
  678. host0 = le16_to_cpu(header->host0);
  679. host1 = le16_to_cpu(header->host1);
  680. if (memcmp(header->magic, SMEM_PART_MAGIC,
  681. sizeof(header->magic))) {
  682. dev_err(smem->dev,
  683. "Partition %d has invalid magic\n", i);
  684. return -EINVAL;
  685. }
  686. if (host0 != local_host && host1 != local_host) {
  687. dev_err(smem->dev,
  688. "Partition %d hosts are invalid\n", i);
  689. return -EINVAL;
  690. }
  691. if (host0 != remote_host && host1 != remote_host) {
  692. dev_err(smem->dev,
  693. "Partition %d hosts are invalid\n", i);
  694. return -EINVAL;
  695. }
  696. if (le32_to_cpu(header->size) != le32_to_cpu(entry->size)) {
  697. dev_err(smem->dev,
  698. "Partition %d has invalid size\n", i);
  699. return -EINVAL;
  700. }
  701. if (le32_to_cpu(header->offset_free_uncached) > le32_to_cpu(header->size)) {
  702. dev_err(smem->dev,
  703. "Partition %d has invalid free pointer\n", i);
  704. return -EINVAL;
  705. }
  706. smem->partitions[remote_host] = header;
  707. smem->cacheline[remote_host] = le32_to_cpu(entry->cacheline);
  708. }
  709. return 0;
  710. }
  711. static int qcom_smem_map_memory(struct qcom_smem *smem, struct device *dev,
  712. const char *name, int i)
  713. {
  714. struct device_node *np;
  715. struct resource r;
  716. int ret;
  717. np = of_parse_phandle(dev->of_node, name, 0);
  718. if (!np) {
  719. dev_err(dev, "No %s specified\n", name);
  720. return -EINVAL;
  721. }
  722. ret = of_address_to_resource(np, 0, &r);
  723. of_node_put(np);
  724. if (ret)
  725. return ret;
  726. smem->regions[i].aux_base = (u32)r.start;
  727. smem->regions[i].size = resource_size(&r);
  728. smem->regions[i].virt_base = devm_ioremap_wc(dev, r.start, resource_size(&r));
  729. if (!smem->regions[i].virt_base)
  730. return -ENOMEM;
  731. return 0;
  732. }
  733. static int qcom_smem_probe(struct platform_device *pdev)
  734. {
  735. struct smem_header *header;
  736. struct qcom_smem *smem;
  737. size_t array_size;
  738. int num_regions;
  739. int hwlock_id;
  740. u32 version;
  741. int ret;
  742. num_regions = 1;
  743. if (of_find_property(pdev->dev.of_node, "qcom,rpm-msg-ram", NULL))
  744. num_regions++;
  745. array_size = num_regions * sizeof(struct smem_region);
  746. smem = devm_kzalloc(&pdev->dev, sizeof(*smem) + array_size, GFP_KERNEL);
  747. if (!smem)
  748. return -ENOMEM;
  749. smem->dev = &pdev->dev;
  750. smem->num_regions = num_regions;
  751. ret = qcom_smem_map_memory(smem, &pdev->dev, "memory-region", 0);
  752. if (ret)
  753. return ret;
  754. if (num_regions > 1 && (ret = qcom_smem_map_memory(smem, &pdev->dev,
  755. "qcom,rpm-msg-ram", 1)))
  756. return ret;
  757. header = smem->regions[0].virt_base;
  758. if (le32_to_cpu(header->initialized) != 1 ||
  759. le32_to_cpu(header->reserved)) {
  760. dev_err(&pdev->dev, "SMEM is not initialized by SBL\n");
  761. return -EINVAL;
  762. }
  763. version = qcom_smem_get_sbl_version(smem);
  764. switch (version >> 16) {
  765. case SMEM_GLOBAL_PART_VERSION:
  766. ret = qcom_smem_set_global_partition(smem);
  767. if (ret < 0)
  768. return ret;
  769. smem->item_count = qcom_smem_get_item_count(smem);
  770. break;
  771. case SMEM_GLOBAL_HEAP_VERSION:
  772. smem->item_count = SMEM_ITEM_COUNT;
  773. break;
  774. default:
  775. dev_err(&pdev->dev, "Unsupported SMEM version 0x%x\n", version);
  776. return -EINVAL;
  777. }
  778. ret = qcom_smem_enumerate_partitions(smem, SMEM_HOST_APPS);
  779. if (ret < 0 && ret != -ENOENT)
  780. return ret;
  781. hwlock_id = of_hwspin_lock_get_id(pdev->dev.of_node, 0);
  782. if (hwlock_id < 0) {
  783. if (hwlock_id != -EPROBE_DEFER)
  784. dev_err(&pdev->dev, "failed to retrieve hwlock\n");
  785. return hwlock_id;
  786. }
  787. smem->hwlock = hwspin_lock_request_specific(hwlock_id);
  788. if (!smem->hwlock)
  789. return -ENXIO;
  790. __smem = smem;
  791. return 0;
  792. }
  793. static int qcom_smem_remove(struct platform_device *pdev)
  794. {
  795. hwspin_lock_free(__smem->hwlock);
  796. __smem = NULL;
  797. return 0;
  798. }
  799. static const struct of_device_id qcom_smem_of_match[] = {
  800. { .compatible = "qcom,smem" },
  801. {}
  802. };
  803. MODULE_DEVICE_TABLE(of, qcom_smem_of_match);
  804. static struct platform_driver qcom_smem_driver = {
  805. .probe = qcom_smem_probe,
  806. .remove = qcom_smem_remove,
  807. .driver = {
  808. .name = "qcom-smem",
  809. .of_match_table = qcom_smem_of_match,
  810. .suppress_bind_attrs = true,
  811. },
  812. };
  813. static int __init qcom_smem_init(void)
  814. {
  815. return platform_driver_register(&qcom_smem_driver);
  816. }
  817. arch_initcall(qcom_smem_init);
  818. static void __exit qcom_smem_exit(void)
  819. {
  820. platform_driver_unregister(&qcom_smem_driver);
  821. }
  822. module_exit(qcom_smem_exit)
  823. MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
  824. MODULE_DESCRIPTION("Qualcomm Shared Memory Manager");
  825. MODULE_LICENSE("GPL v2");