iosys-map.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Pointer abstraction for IO/system memory
  4. */
  5. #ifndef __IOSYS_MAP_H__
  6. #define __IOSYS_MAP_H__
  7. #include <linux/compiler_types.h>
  8. #include <linux/io.h>
  9. #include <linux/string.h>
  10. /**
  11. * DOC: overview
  12. *
  13. * When accessing a memory region, depending on its location, users may have to
  14. * access it with I/O operations or memory load/store operations. For example,
  15. * copying to system memory could be done with memcpy(), copying to I/O memory
  16. * would be done with memcpy_toio().
  17. *
  18. * .. code-block:: c
  19. *
  20. * void *vaddr = ...; // pointer to system memory
  21. * memcpy(vaddr, src, len);
  22. *
  23. * void *vaddr_iomem = ...; // pointer to I/O memory
  24. * memcpy_toio(vaddr_iomem, src, len);
  25. *
  26. * The user of such pointer may not have information about the mapping of that
  27. * region or may want to have a single code path to handle operations on that
  28. * buffer, regardless if it's located in system or IO memory. The type
  29. * :c:type:`struct iosys_map <iosys_map>` and its helpers abstract that so the
  30. * buffer can be passed around to other drivers or have separate duties inside
  31. * the same driver for allocation, read and write operations.
  32. *
  33. * Open-coding access to :c:type:`struct iosys_map <iosys_map>` is considered
  34. * bad style. Rather then accessing its fields directly, use one of the provided
  35. * helper functions, or implement your own. For example, instances of
  36. * :c:type:`struct iosys_map <iosys_map>` can be initialized statically with
  37. * IOSYS_MAP_INIT_VADDR(), or at runtime with iosys_map_set_vaddr(). These
  38. * helpers will set an address in system memory.
  39. *
  40. * .. code-block:: c
  41. *
  42. * struct iosys_map map = IOSYS_MAP_INIT_VADDR(0xdeadbeaf);
  43. *
  44. * iosys_map_set_vaddr(&map, 0xdeadbeaf);
  45. *
  46. * To set an address in I/O memory, use IOSYS_MAP_INIT_VADDR_IOMEM() or
  47. * iosys_map_set_vaddr_iomem().
  48. *
  49. * .. code-block:: c
  50. *
  51. * struct iosys_map map = IOSYS_MAP_INIT_VADDR_IOMEM(0xdeadbeaf);
  52. *
  53. * iosys_map_set_vaddr_iomem(&map, 0xdeadbeaf);
  54. *
  55. * Instances of struct iosys_map do not have to be cleaned up, but
  56. * can be cleared to NULL with iosys_map_clear(). Cleared mappings
  57. * always refer to system memory.
  58. *
  59. * .. code-block:: c
  60. *
  61. * iosys_map_clear(&map);
  62. *
  63. * Test if a mapping is valid with either iosys_map_is_set() or
  64. * iosys_map_is_null().
  65. *
  66. * .. code-block:: c
  67. *
  68. * if (iosys_map_is_set(&map) != iosys_map_is_null(&map))
  69. * // always true
  70. *
  71. * Instances of :c:type:`struct iosys_map <iosys_map>` can be compared for
  72. * equality with iosys_map_is_equal(). Mappings that point to different memory
  73. * spaces, system or I/O, are never equal. That's even true if both spaces are
  74. * located in the same address space, both mappings contain the same address
  75. * value, or both mappings refer to NULL.
  76. *
  77. * .. code-block:: c
  78. *
  79. * struct iosys_map sys_map; // refers to system memory
  80. * struct iosys_map io_map; // refers to I/O memory
  81. *
  82. * if (iosys_map_is_equal(&sys_map, &io_map))
  83. * // always false
  84. *
  85. * A set up instance of struct iosys_map can be used to access or manipulate the
  86. * buffer memory. Depending on the location of the memory, the provided helpers
  87. * will pick the correct operations. Data can be copied into the memory with
  88. * iosys_map_memcpy_to(). The address can be manipulated with iosys_map_incr().
  89. *
  90. * .. code-block:: c
  91. *
  92. * const void *src = ...; // source buffer
  93. * size_t len = ...; // length of src
  94. *
  95. * iosys_map_memcpy_to(&map, src, len);
  96. * iosys_map_incr(&map, len); // go to first byte after the memcpy
  97. */
  98. /**
  99. * struct iosys_map - Pointer to IO/system memory
  100. * @vaddr_iomem: The buffer's address if in I/O memory
  101. * @vaddr: The buffer's address if in system memory
  102. * @is_iomem: True if the buffer is located in I/O memory, or false
  103. * otherwise.
  104. */
  105. struct iosys_map {
  106. union {
  107. void __iomem *vaddr_iomem;
  108. void *vaddr;
  109. };
  110. bool is_iomem;
  111. };
  112. /**
  113. * IOSYS_MAP_INIT_VADDR - Initializes struct iosys_map to an address in system memory
  114. * @vaddr_: A system-memory address
  115. */
  116. #define IOSYS_MAP_INIT_VADDR(vaddr_) \
  117. { \
  118. .vaddr = (vaddr_), \
  119. .is_iomem = false, \
  120. }
  121. /**
  122. * IOSYS_MAP_INIT_VADDR_IOMEM - Initializes struct iosys_map to an address in I/O memory
  123. * @vaddr_iomem_: An I/O-memory address
  124. */
  125. #define IOSYS_MAP_INIT_VADDR_IOMEM(vaddr_iomem_) \
  126. { \
  127. .vaddr_iomem = (vaddr_iomem_), \
  128. .is_iomem = true, \
  129. }
  130. /**
  131. * IOSYS_MAP_INIT_OFFSET - Initializes struct iosys_map from another iosys_map
  132. * @map_: The dma-buf mapping structure to copy from
  133. * @offset_: Offset to add to the other mapping
  134. *
  135. * Initializes a new iosys_map struct based on another passed as argument. It
  136. * does a shallow copy of the struct so it's possible to update the back storage
  137. * without changing where the original map points to. It is the equivalent of
  138. * doing:
  139. *
  140. * .. code-block:: c
  141. *
  142. * iosys_map map = other_map;
  143. * iosys_map_incr(&map, &offset);
  144. *
  145. * Example usage:
  146. *
  147. * .. code-block:: c
  148. *
  149. * void foo(struct device *dev, struct iosys_map *base_map)
  150. * {
  151. * ...
  152. * struct iosys_map map = IOSYS_MAP_INIT_OFFSET(base_map, FIELD_OFFSET);
  153. * ...
  154. * }
  155. *
  156. * The advantage of using the initializer over just increasing the offset with
  157. * iosys_map_incr() like above is that the new map will always point to the
  158. * right place of the buffer during its scope. It reduces the risk of updating
  159. * the wrong part of the buffer and having no compiler warning about that. If
  160. * the assignment to IOSYS_MAP_INIT_OFFSET() is forgotten, the compiler can warn
  161. * about the use of uninitialized variable.
  162. */
  163. #define IOSYS_MAP_INIT_OFFSET(map_, offset_) ({ \
  164. struct iosys_map copy = *map_; \
  165. iosys_map_incr(&copy, offset_); \
  166. copy; \
  167. })
  168. /**
  169. * iosys_map_set_vaddr - Sets a iosys mapping structure to an address in system memory
  170. * @map: The iosys_map structure
  171. * @vaddr: A system-memory address
  172. *
  173. * Sets the address and clears the I/O-memory flag.
  174. */
  175. static inline void iosys_map_set_vaddr(struct iosys_map *map, void *vaddr)
  176. {
  177. map->vaddr = vaddr;
  178. map->is_iomem = false;
  179. }
  180. /**
  181. * iosys_map_set_vaddr_iomem - Sets a iosys mapping structure to an address in I/O memory
  182. * @map: The iosys_map structure
  183. * @vaddr_iomem: An I/O-memory address
  184. *
  185. * Sets the address and the I/O-memory flag.
  186. */
  187. static inline void iosys_map_set_vaddr_iomem(struct iosys_map *map,
  188. void __iomem *vaddr_iomem)
  189. {
  190. map->vaddr_iomem = vaddr_iomem;
  191. map->is_iomem = true;
  192. }
  193. /**
  194. * iosys_map_is_equal - Compares two iosys mapping structures for equality
  195. * @lhs: The iosys_map structure
  196. * @rhs: A iosys_map structure to compare with
  197. *
  198. * Two iosys mapping structures are equal if they both refer to the same type of memory
  199. * and to the same address within that memory.
  200. *
  201. * Returns:
  202. * True is both structures are equal, or false otherwise.
  203. */
  204. static inline bool iosys_map_is_equal(const struct iosys_map *lhs,
  205. const struct iosys_map *rhs)
  206. {
  207. if (lhs->is_iomem != rhs->is_iomem)
  208. return false;
  209. else if (lhs->is_iomem)
  210. return lhs->vaddr_iomem == rhs->vaddr_iomem;
  211. else
  212. return lhs->vaddr == rhs->vaddr;
  213. }
  214. /**
  215. * iosys_map_is_null - Tests for a iosys mapping to be NULL
  216. * @map: The iosys_map structure
  217. *
  218. * Depending on the state of struct iosys_map.is_iomem, tests if the
  219. * mapping is NULL.
  220. *
  221. * Returns:
  222. * True if the mapping is NULL, or false otherwise.
  223. */
  224. static inline bool iosys_map_is_null(const struct iosys_map *map)
  225. {
  226. if (map->is_iomem)
  227. return !map->vaddr_iomem;
  228. return !map->vaddr;
  229. }
  230. /**
  231. * iosys_map_is_set - Tests if the iosys mapping has been set
  232. * @map: The iosys_map structure
  233. *
  234. * Depending on the state of struct iosys_map.is_iomem, tests if the
  235. * mapping has been set.
  236. *
  237. * Returns:
  238. * True if the mapping is been set, or false otherwise.
  239. */
  240. static inline bool iosys_map_is_set(const struct iosys_map *map)
  241. {
  242. return !iosys_map_is_null(map);
  243. }
  244. /**
  245. * iosys_map_clear - Clears a iosys mapping structure
  246. * @map: The iosys_map structure
  247. *
  248. * Clears all fields to zero, including struct iosys_map.is_iomem, so
  249. * mapping structures that were set to point to I/O memory are reset for
  250. * system memory. Pointers are cleared to NULL. This is the default.
  251. */
  252. static inline void iosys_map_clear(struct iosys_map *map)
  253. {
  254. if (map->is_iomem) {
  255. map->vaddr_iomem = NULL;
  256. map->is_iomem = false;
  257. } else {
  258. map->vaddr = NULL;
  259. }
  260. }
  261. /**
  262. * iosys_map_memcpy_to - Memcpy into offset of iosys_map
  263. * @dst: The iosys_map structure
  264. * @dst_offset: The offset from which to copy
  265. * @src: The source buffer
  266. * @len: The number of byte in src
  267. *
  268. * Copies data into a iosys_map with an offset. The source buffer is in
  269. * system memory. Depending on the buffer's location, the helper picks the
  270. * correct method of accessing the memory.
  271. */
  272. static inline void iosys_map_memcpy_to(struct iosys_map *dst, size_t dst_offset,
  273. const void *src, size_t len)
  274. {
  275. if (dst->is_iomem)
  276. memcpy_toio(dst->vaddr_iomem + dst_offset, src, len);
  277. else
  278. memcpy(dst->vaddr + dst_offset, src, len);
  279. }
  280. /**
  281. * iosys_map_memcpy_from - Memcpy from iosys_map into system memory
  282. * @dst: Destination in system memory
  283. * @src: The iosys_map structure
  284. * @src_offset: The offset from which to copy
  285. * @len: The number of byte in src
  286. *
  287. * Copies data from a iosys_map with an offset. The dest buffer is in
  288. * system memory. Depending on the mapping location, the helper picks the
  289. * correct method of accessing the memory.
  290. */
  291. static inline void iosys_map_memcpy_from(void *dst, const struct iosys_map *src,
  292. size_t src_offset, size_t len)
  293. {
  294. if (src->is_iomem)
  295. memcpy_fromio(dst, src->vaddr_iomem + src_offset, len);
  296. else
  297. memcpy(dst, src->vaddr + src_offset, len);
  298. }
  299. /**
  300. * iosys_map_incr - Increments the address stored in a iosys mapping
  301. * @map: The iosys_map structure
  302. * @incr: The number of bytes to increment
  303. *
  304. * Increments the address stored in a iosys mapping. Depending on the
  305. * buffer's location, the correct value will be updated.
  306. */
  307. static inline void iosys_map_incr(struct iosys_map *map, size_t incr)
  308. {
  309. if (map->is_iomem)
  310. map->vaddr_iomem += incr;
  311. else
  312. map->vaddr += incr;
  313. }
  314. /**
  315. * iosys_map_memset - Memset iosys_map
  316. * @dst: The iosys_map structure
  317. * @offset: Offset from dst where to start setting value
  318. * @value: The value to set
  319. * @len: The number of bytes to set in dst
  320. *
  321. * Set value in iosys_map. Depending on the buffer's location, the helper
  322. * picks the correct method of accessing the memory.
  323. */
  324. static inline void iosys_map_memset(struct iosys_map *dst, size_t offset,
  325. int value, size_t len)
  326. {
  327. if (dst->is_iomem)
  328. memset_io(dst->vaddr_iomem + offset, value, len);
  329. else
  330. memset(dst->vaddr + offset, value, len);
  331. }
  332. #ifdef CONFIG_64BIT
  333. #define __iosys_map_rd_io_u64_case(val_, vaddr_iomem_) \
  334. u64: val_ = readq(vaddr_iomem_)
  335. #define __iosys_map_wr_io_u64_case(val_, vaddr_iomem_) \
  336. u64: writeq(val_, vaddr_iomem_)
  337. #else
  338. #define __iosys_map_rd_io_u64_case(val_, vaddr_iomem_) \
  339. u64: memcpy_fromio(&(val_), vaddr_iomem_, sizeof(u64))
  340. #define __iosys_map_wr_io_u64_case(val_, vaddr_iomem_) \
  341. u64: memcpy_toio(vaddr_iomem_, &(val_), sizeof(u64))
  342. #endif
  343. #define __iosys_map_rd_io(val__, vaddr_iomem__, type__) _Generic(val__, \
  344. u8: val__ = readb(vaddr_iomem__), \
  345. u16: val__ = readw(vaddr_iomem__), \
  346. u32: val__ = readl(vaddr_iomem__), \
  347. __iosys_map_rd_io_u64_case(val__, vaddr_iomem__))
  348. #define __iosys_map_rd_sys(val__, vaddr__, type__) \
  349. val__ = READ_ONCE(*(type__ *)(vaddr__))
  350. #define __iosys_map_wr_io(val__, vaddr_iomem__, type__) _Generic(val__, \
  351. u8: writeb(val__, vaddr_iomem__), \
  352. u16: writew(val__, vaddr_iomem__), \
  353. u32: writel(val__, vaddr_iomem__), \
  354. __iosys_map_wr_io_u64_case(val__, vaddr_iomem__))
  355. #define __iosys_map_wr_sys(val__, vaddr__, type__) \
  356. WRITE_ONCE(*(type__ *)(vaddr__), val__)
  357. /**
  358. * iosys_map_rd - Read a C-type value from the iosys_map
  359. *
  360. * @map__: The iosys_map structure
  361. * @offset__: The offset from which to read
  362. * @type__: Type of the value being read
  363. *
  364. * Read a C type value (u8, u16, u32 and u64) from iosys_map. For other types or
  365. * if pointer may be unaligned (and problematic for the architecture supported),
  366. * use iosys_map_memcpy_from().
  367. *
  368. * Returns:
  369. * The value read from the mapping.
  370. */
  371. #define iosys_map_rd(map__, offset__, type__) ({ \
  372. type__ val; \
  373. if ((map__)->is_iomem) { \
  374. __iosys_map_rd_io(val, (map__)->vaddr_iomem + (offset__), type__);\
  375. } else { \
  376. __iosys_map_rd_sys(val, (map__)->vaddr + (offset__), type__); \
  377. } \
  378. val; \
  379. })
  380. /**
  381. * iosys_map_wr - Write a C-type value to the iosys_map
  382. *
  383. * @map__: The iosys_map structure
  384. * @offset__: The offset from the mapping to write to
  385. * @type__: Type of the value being written
  386. * @val__: Value to write
  387. *
  388. * Write a C type value (u8, u16, u32 and u64) to the iosys_map. For other types
  389. * or if pointer may be unaligned (and problematic for the architecture
  390. * supported), use iosys_map_memcpy_to()
  391. */
  392. #define iosys_map_wr(map__, offset__, type__, val__) ({ \
  393. type__ val = (val__); \
  394. if ((map__)->is_iomem) { \
  395. __iosys_map_wr_io(val, (map__)->vaddr_iomem + (offset__), type__);\
  396. } else { \
  397. __iosys_map_wr_sys(val, (map__)->vaddr + (offset__), type__); \
  398. } \
  399. })
  400. /**
  401. * iosys_map_rd_field - Read a member from a struct in the iosys_map
  402. *
  403. * @map__: The iosys_map structure
  404. * @struct_offset__: Offset from the beggining of the map, where the struct
  405. * is located
  406. * @struct_type__: The struct describing the layout of the mapping
  407. * @field__: Member of the struct to read
  408. *
  409. * Read a value from iosys_map considering its layout is described by a C struct
  410. * starting at @struct_offset__. The field offset and size is calculated and its
  411. * value read. If the field access would incur in un-aligned access, then either
  412. * iosys_map_memcpy_from() needs to be used or the architecture must support it.
  413. * For example: suppose there is a @struct foo defined as below and the value
  414. * ``foo.field2.inner2`` needs to be read from the iosys_map:
  415. *
  416. * .. code-block:: c
  417. *
  418. * struct foo {
  419. * int field1;
  420. * struct {
  421. * int inner1;
  422. * int inner2;
  423. * } field2;
  424. * int field3;
  425. * } __packed;
  426. *
  427. * This is the expected memory layout of a buffer using iosys_map_rd_field():
  428. *
  429. * +------------------------------+--------------------------+
  430. * | Address | Content |
  431. * +==============================+==========================+
  432. * | buffer + 0000 | start of mmapped buffer |
  433. * | | pointed by iosys_map |
  434. * +------------------------------+--------------------------+
  435. * | ... | ... |
  436. * +------------------------------+--------------------------+
  437. * | buffer + ``struct_offset__`` | start of ``struct foo`` |
  438. * +------------------------------+--------------------------+
  439. * | ... | ... |
  440. * +------------------------------+--------------------------+
  441. * | buffer + wwww | ``foo.field2.inner2`` |
  442. * +------------------------------+--------------------------+
  443. * | ... | ... |
  444. * +------------------------------+--------------------------+
  445. * | buffer + yyyy | end of ``struct foo`` |
  446. * +------------------------------+--------------------------+
  447. * | ... | ... |
  448. * +------------------------------+--------------------------+
  449. * | buffer + zzzz | end of mmaped buffer |
  450. * +------------------------------+--------------------------+
  451. *
  452. * Values automatically calculated by this macro or not needed are denoted by
  453. * wwww, yyyy and zzzz. This is the code to read that value:
  454. *
  455. * .. code-block:: c
  456. *
  457. * x = iosys_map_rd_field(&map, offset, struct foo, field2.inner2);
  458. *
  459. * Returns:
  460. * The value read from the mapping.
  461. */
  462. #define iosys_map_rd_field(map__, struct_offset__, struct_type__, field__) ({ \
  463. struct_type__ *s; \
  464. iosys_map_rd(map__, struct_offset__ + offsetof(struct_type__, field__), \
  465. typeof(s->field__)); \
  466. })
  467. /**
  468. * iosys_map_wr_field - Write to a member of a struct in the iosys_map
  469. *
  470. * @map__: The iosys_map structure
  471. * @struct_offset__: Offset from the beggining of the map, where the struct
  472. * is located
  473. * @struct_type__: The struct describing the layout of the mapping
  474. * @field__: Member of the struct to read
  475. * @val__: Value to write
  476. *
  477. * Write a value to the iosys_map considering its layout is described by a C
  478. * struct starting at @struct_offset__. The field offset and size is calculated
  479. * and the @val__ is written. If the field access would incur in un-aligned
  480. * access, then either iosys_map_memcpy_to() needs to be used or the
  481. * architecture must support it. Refer to iosys_map_rd_field() for expected
  482. * usage and memory layout.
  483. */
  484. #define iosys_map_wr_field(map__, struct_offset__, struct_type__, field__, val__) ({ \
  485. struct_type__ *s; \
  486. iosys_map_wr(map__, struct_offset__ + offsetof(struct_type__, field__), \
  487. typeof(s->field__), val__); \
  488. })
  489. #endif /* __IOSYS_MAP_H__ */