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