msm_smem.c 24 KB

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