msm_smem.c 24 KB

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