msm_smem.c 24 KB

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