msm_smem.c 24 KB

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