csr1212.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /*
  2. * csr1212.h -- IEEE 1212 Control and Status Register support for Linux
  3. *
  4. * Copyright (C) 2003 Francois Retief <fgretief@sun.ac.za>
  5. * Steve Kinneberg <kinnebergsteve@acmsystems.com>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  19. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  21. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  24. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  26. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  27. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef __CSR1212_H__
  30. #define __CSR1212_H__
  31. /* Compatibility layer */
  32. #ifdef __KERNEL__
  33. #include <linux/types.h>
  34. #include <linux/slab.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/vmalloc.h>
  37. #include <asm/pgalloc.h>
  38. #define CSR1212_MALLOC(size) vmalloc((size))
  39. #define CSR1212_FREE(ptr) vfree(ptr)
  40. #define CSR1212_BE16_TO_CPU(quad) be16_to_cpu(quad)
  41. #define CSR1212_CPU_TO_BE16(quad) cpu_to_be16(quad)
  42. #define CSR1212_BE32_TO_CPU(quad) be32_to_cpu(quad)
  43. #define CSR1212_CPU_TO_BE32(quad) cpu_to_be32(quad)
  44. #define CSR1212_BE64_TO_CPU(quad) be64_to_cpu(quad)
  45. #define CSR1212_CPU_TO_BE64(quad) cpu_to_be64(quad)
  46. #define CSR1212_LE16_TO_CPU(quad) le16_to_cpu(quad)
  47. #define CSR1212_CPU_TO_LE16(quad) cpu_to_le16(quad)
  48. #define CSR1212_LE32_TO_CPU(quad) le32_to_cpu(quad)
  49. #define CSR1212_CPU_TO_LE32(quad) cpu_to_le32(quad)
  50. #define CSR1212_LE64_TO_CPU(quad) le64_to_cpu(quad)
  51. #define CSR1212_CPU_TO_LE64(quad) cpu_to_le64(quad)
  52. #include <linux/errno.h>
  53. #define CSR1212_SUCCESS (0)
  54. #define CSR1212_EINVAL (-EINVAL)
  55. #define CSR1212_ENOMEM (-ENOMEM)
  56. #define CSR1212_ENOENT (-ENOENT)
  57. #define CSR1212_EIO (-EIO)
  58. #define CSR1212_EBUSY (-EBUSY)
  59. #else /* Userspace */
  60. #include <sys/types.h>
  61. #include <malloc.h>
  62. #define CSR1212_MALLOC(size) malloc(size)
  63. #define CSR1212_FREE(ptr) free(ptr)
  64. #include <endian.h>
  65. #if __BYTE_ORDER == __LITTLE_ENDIAN
  66. #include <byteswap.h>
  67. #define CSR1212_BE16_TO_CPU(quad) bswap_16(quad)
  68. #define CSR1212_CPU_TO_BE16(quad) bswap_16(quad)
  69. #define CSR1212_BE32_TO_CPU(quad) bswap_32(quad)
  70. #define CSR1212_CPU_TO_BE32(quad) bswap_32(quad)
  71. #define CSR1212_BE64_TO_CPU(quad) bswap_64(quad)
  72. #define CSR1212_CPU_TO_BE64(quad) bswap_64(quad)
  73. #define CSR1212_LE16_TO_CPU(quad) (quad)
  74. #define CSR1212_CPU_TO_LE16(quad) (quad)
  75. #define CSR1212_LE32_TO_CPU(quad) (quad)
  76. #define CSR1212_CPU_TO_LE32(quad) (quad)
  77. #define CSR1212_LE64_TO_CPU(quad) (quad)
  78. #define CSR1212_CPU_TO_LE64(quad) (quad)
  79. #else
  80. #define CSR1212_BE16_TO_CPU(quad) (quad)
  81. #define CSR1212_CPU_TO_BE16(quad) (quad)
  82. #define CSR1212_BE32_TO_CPU(quad) (quad)
  83. #define CSR1212_CPU_TO_BE32(quad) (quad)
  84. #define CSR1212_BE64_TO_CPU(quad) (quad)
  85. #define CSR1212_CPU_TO_BE64(quad) (quad)
  86. #define CSR1212_LE16_TO_CPU(quad) bswap_16(quad)
  87. #define CSR1212_CPU_TO_LE16(quad) bswap_16(quad)
  88. #define CSR1212_LE32_TO_CPU(quad) bswap_32(quad)
  89. #define CSR1212_CPU_TO_LE32(quad) bswap_32(quad)
  90. #define CSR1212_LE64_TO_CPU(quad) bswap_64(quad)
  91. #define CSR1212_CPU_TO_LE64(quad) bswap_64(quad)
  92. #endif
  93. #include <errno.h>
  94. #define CSR1212_SUCCESS (0)
  95. #define CSR1212_EINVAL (EINVAL)
  96. #define CSR1212_ENOMEM (ENOMEM)
  97. #define CSR1212_ENOENT (ENOENT)
  98. #define CSR1212_EIO (EIO)
  99. #define CSR1212_EBUSY (EBUSY)
  100. #endif
  101. #define CSR1212_KV_VAL_MASK 0xffffff
  102. #define CSR1212_KV_KEY_SHIFT 24
  103. #define CSR1212_KV_KEY_TYPE_SHIFT 6
  104. #define CSR1212_KV_KEY_ID_MASK 0x3f
  105. #define CSR1212_KV_KEY_TYPE_MASK 0x3 /* After shift */
  106. /* CSR 1212 key types */
  107. #define CSR1212_KV_TYPE_IMMEDIATE 0
  108. #define CSR1212_KV_TYPE_CSR_OFFSET 1
  109. #define CSR1212_KV_TYPE_LEAF 2
  110. #define CSR1212_KV_TYPE_DIRECTORY 3
  111. /* CSR 1212 key ids */
  112. #define CSR1212_KV_ID_DESCRIPTOR 0x01
  113. #define CSR1212_KV_ID_BUS_DEPENDENT_INFO 0x02
  114. #define CSR1212_KV_ID_VENDOR 0x03
  115. #define CSR1212_KV_ID_HARDWARE_VERSION 0x04
  116. #define CSR1212_KV_ID_MODULE 0x07
  117. #define CSR1212_KV_ID_NODE_CAPABILITIES 0x0C
  118. #define CSR1212_KV_ID_EUI_64 0x0D
  119. #define CSR1212_KV_ID_UNIT 0x11
  120. #define CSR1212_KV_ID_SPECIFIER_ID 0x12
  121. #define CSR1212_KV_ID_VERSION 0x13
  122. #define CSR1212_KV_ID_DEPENDENT_INFO 0x14
  123. #define CSR1212_KV_ID_UNIT_LOCATION 0x15
  124. #define CSR1212_KV_ID_MODEL 0x17
  125. #define CSR1212_KV_ID_INSTANCE 0x18
  126. #define CSR1212_KV_ID_KEYWORD 0x19
  127. #define CSR1212_KV_ID_FEATURE 0x1A
  128. #define CSR1212_KV_ID_EXTENDED_ROM 0x1B
  129. #define CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID 0x1C
  130. #define CSR1212_KV_ID_EXTENDED_KEY 0x1D
  131. #define CSR1212_KV_ID_EXTENDED_DATA 0x1E
  132. #define CSR1212_KV_ID_MODIFIABLE_DESCRIPTOR 0x1F
  133. #define CSR1212_KV_ID_DIRECTORY_ID 0x20
  134. #define CSR1212_KV_ID_REVISION 0x21
  135. /* IEEE 1212 Address space map */
  136. #define CSR1212_ALL_SPACE_BASE (0x000000000000ULL)
  137. #define CSR1212_ALL_SPACE_SIZE (1ULL << 48)
  138. #define CSR1212_ALL_SPACE_END (CSR1212_ALL_SPACE_BASE + CSR1212_ALL_SPACE_SIZE)
  139. #define CSR1212_MEMORY_SPACE_BASE (0x000000000000ULL)
  140. #define CSR1212_MEMORY_SPACE_SIZE ((256ULL * (1ULL << 40)) - (512ULL * (1ULL << 20)))
  141. #define CSR1212_MEMORY_SPACE_END (CSR1212_MEMORY_SPACE_BASE + CSR1212_MEMORY_SPACE_SIZE)
  142. #define CSR1212_PRIVATE_SPACE_BASE (0xffffe0000000ULL)
  143. #define CSR1212_PRIVATE_SPACE_SIZE (256ULL * (1ULL << 20))
  144. #define CSR1212_PRIVATE_SPACE_END (CSR1212_PRIVATE_SPACE_BASE + CSR1212_PRIVATE_SPACE_SIZE)
  145. #define CSR1212_REGISTER_SPACE_BASE (0xfffff0000000ULL)
  146. #define CSR1212_REGISTER_SPACE_SIZE (256ULL * (1ULL << 20))
  147. #define CSR1212_REGISTER_SPACE_END (CSR1212_REGISTER_SPACE_BASE + CSR1212_REGISTER_SPACE_SIZE)
  148. #define CSR1212_CSR_ARCH_REG_SPACE_BASE (0xfffff0000000ULL)
  149. #define CSR1212_CSR_ARCH_REG_SPACE_SIZE (512)
  150. #define CSR1212_CSR_ARCH_REG_SPACE_END (CSR1212_CSR_ARCH_REG_SPACE_BASE + CSR1212_CSR_ARCH_REG_SPACE_SIZE)
  151. #define CSR1212_CSR_ARCH_REG_SPACE_OFFSET (CSR1212_CSR_ARCH_REG_SPACE_BASE - CSR1212_REGISTER_SPACE_BASE)
  152. #define CSR1212_CSR_BUS_DEP_REG_SPACE_BASE (0xfffff0000200ULL)
  153. #define CSR1212_CSR_BUS_DEP_REG_SPACE_SIZE (512)
  154. #define CSR1212_CSR_BUS_DEP_REG_SPACE_END (CSR1212_CSR_BUS_DEP_REG_SPACE_BASE + CSR1212_CSR_BUS_DEP_REG_SPACE_SIZE)
  155. #define CSR1212_CSR_BUS_DEP_REG_SPACE_OFFSET (CSR1212_CSR_BUS_DEP_REG_SPACE_BASE - CSR1212_REGISTER_SPACE_BASE)
  156. #define CSR1212_CONFIG_ROM_SPACE_BASE (0xfffff0000400ULL)
  157. #define CSR1212_CONFIG_ROM_SPACE_SIZE (1024)
  158. #define CSR1212_CONFIG_ROM_SPACE_END (CSR1212_CONFIG_ROM_SPACE_BASE + CSR1212_CONFIG_ROM_SPACE_SIZE)
  159. #define CSR1212_CONFIG_ROM_SPACE_OFFSET (CSR1212_CONFIG_ROM_SPACE_BASE - CSR1212_REGISTER_SPACE_BASE)
  160. #define CSR1212_UNITS_SPACE_BASE (0xfffff0000800ULL)
  161. #define CSR1212_UNITS_SPACE_SIZE ((256ULL * (1ULL << 20)) - 2048)
  162. #define CSR1212_UNITS_SPACE_END (CSR1212_UNITS_SPACE_BASE + CSR1212_UNITS_SPACE_SIZE)
  163. #define CSR1212_UNITS_SPACE_OFFSET (CSR1212_UNITS_SPACE_BASE - CSR1212_REGISTER_SPACE_BASE)
  164. #define CSR1212_EXTENDED_ROM_SIZE (0x10000 * sizeof(u_int32_t))
  165. /* Config ROM image structures */
  166. struct csr1212_bus_info_block_img {
  167. u_int8_t length;
  168. u_int8_t crc_length;
  169. u_int16_t crc;
  170. /* Must be last */
  171. u_int32_t data[0]; /* older gcc can't handle [] which is standard */
  172. };
  173. #define CSR1212_KV_KEY(quad) (CSR1212_BE32_TO_CPU(quad) >> CSR1212_KV_KEY_SHIFT)
  174. #define CSR1212_KV_KEY_TYPE(quad) (CSR1212_KV_KEY(quad) >> CSR1212_KV_KEY_TYPE_SHIFT)
  175. #define CSR1212_KV_KEY_ID(quad) (CSR1212_KV_KEY(quad) & CSR1212_KV_KEY_ID_MASK)
  176. #define CSR1212_KV_VAL(quad) (CSR1212_BE32_TO_CPU(quad) & CSR1212_KV_VAL_MASK)
  177. #define CSR1212_SET_KV_KEY(quad, key) ((quad) = \
  178. CSR1212_CPU_TO_BE32(CSR1212_KV_VAL(quad) | ((key) << CSR1212_KV_KEY_SHIFT)))
  179. #define CSR1212_SET_KV_VAL(quad, val) ((quad) = \
  180. CSR1212_CPU_TO_BE32((CSR1212_KV_KEY(quad) << CSR1212_KV_KEY_SHIFT) | (val)))
  181. #define CSR1212_SET_KV_TYPEID(quad, type, id) ((quad) = \
  182. CSR1212_CPU_TO_BE32(CSR1212_KV_VAL(quad) | \
  183. (((((type) & CSR1212_KV_KEY_TYPE_MASK) << CSR1212_KV_KEY_TYPE_SHIFT) | \
  184. ((id) & CSR1212_KV_KEY_ID_MASK)) << CSR1212_KV_KEY_SHIFT)))
  185. typedef u_int32_t csr1212_quad_t;
  186. struct csr1212_keyval_img {
  187. u_int16_t length;
  188. u_int16_t crc;
  189. /* Must be last */
  190. csr1212_quad_t data[0]; /* older gcc can't handle [] which is standard */
  191. };
  192. struct csr1212_leaf {
  193. int len;
  194. u_int32_t *data;
  195. };
  196. struct csr1212_dentry {
  197. struct csr1212_dentry *next, *prev;
  198. struct csr1212_keyval *kv;
  199. };
  200. struct csr1212_directory {
  201. int len;
  202. struct csr1212_dentry *dentries_head, *dentries_tail;
  203. };
  204. struct csr1212_keyval {
  205. struct {
  206. u_int8_t type;
  207. u_int8_t id;
  208. } key;
  209. union {
  210. u_int32_t immediate;
  211. u_int32_t csr_offset;
  212. struct csr1212_leaf leaf;
  213. struct csr1212_directory directory;
  214. } value;
  215. struct csr1212_keyval *associate;
  216. int refcnt;
  217. /* used in generating and/or parsing CSR image */
  218. struct csr1212_keyval *next, *prev; /* flat list of CSR elements */
  219. u_int32_t offset; /* position in CSR from 0xffff f000 0000 */
  220. u_int8_t valid; /* flag indicating keyval has valid data*/
  221. };
  222. struct csr1212_cache_region {
  223. struct csr1212_cache_region *next, *prev;
  224. u_int32_t offset_start; /* inclusive */
  225. u_int32_t offset_end; /* exclusive */
  226. };
  227. struct csr1212_csr_rom_cache {
  228. struct csr1212_csr_rom_cache *next, *prev;
  229. struct csr1212_cache_region *filled_head, *filled_tail;
  230. struct csr1212_keyval *layout_head, *layout_tail;
  231. size_t size;
  232. u_int32_t offset;
  233. struct csr1212_keyval *ext_rom;
  234. size_t len;
  235. /* Must be last */
  236. u_int32_t data[0]; /* older gcc can't handle [] which is standard */
  237. };
  238. struct csr1212_csr {
  239. size_t bus_info_len; /* bus info block length in bytes */
  240. size_t crc_len; /* crc length in bytes */
  241. u_int32_t *bus_info_data; /* bus info data incl bus name and EUI */
  242. void *private; /* private, bus specific data */
  243. struct csr1212_bus_ops *ops;
  244. struct csr1212_keyval *root_kv;
  245. int max_rom; /* max bytes readable in Config ROM region */
  246. /* Items below used for image parsing and generation */
  247. struct csr1212_csr_rom_cache *cache_head, *cache_tail;
  248. };
  249. struct csr1212_bus_ops {
  250. /* This function is used by csr1212 to read additional information
  251. * from remote nodes when parsing a Config ROM (i.e., read Config ROM
  252. * entries located in the Units Space. Must return 0 on success
  253. * anything else indicates an error. */
  254. int (*bus_read) (struct csr1212_csr *csr, u_int64_t addr,
  255. u_int16_t length, void *buffer, void *private);
  256. /* This function is used by csr1212 to allocate a region in units space
  257. * in the event that Config ROM entries don't all fit in the predefined
  258. * 1K region. The void *private parameter is private member of struct
  259. * csr1212_csr. */
  260. u_int64_t (*allocate_addr_range) (u_int64_t size, u_int32_t alignment,
  261. void *private);
  262. /* This function is used by csr1212 to release a region in units space
  263. * that is no longer needed. */
  264. void (*release_addr) (u_int64_t addr, void *private);
  265. /* This function is used by csr1212 to determine the max read request
  266. * supported by a remote node when reading the ConfigROM space. Must
  267. * return 0, 1, or 2 per IEEE 1212. */
  268. int (*get_max_rom) (u_int32_t *bus_info, void *private);
  269. };
  270. /* Descriptor Leaf manipulation macros */
  271. #define CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT 24
  272. #define CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID_MASK 0xffffff
  273. #define CSR1212_DESCRIPTOR_LEAF_OVERHEAD (1 * sizeof(u_int32_t))
  274. #define CSR1212_DESCRIPTOR_LEAF_TYPE(kv) \
  275. (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[0]) >> CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT)
  276. #define CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) \
  277. (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[0]) & \
  278. CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID_MASK)
  279. #define CSR1212_DESCRIPTOR_LEAF_DATA(kv) \
  280. (&((kv)->value.leaf.data[1]))
  281. #define CSR1212_DESCRIPTOR_LEAF_SET_TYPE(kv, type) \
  282. ((kv)->value.leaf.data[0] = \
  283. CSR1212_CPU_TO_BE32(CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) | \
  284. ((type) << CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT)))
  285. #define CSR1212_DESCRIPTOR_LEAF_SET_SPECIFIER_ID(kv, spec_id) \
  286. ((kv)->value.leaf.data[0] = \
  287. CSR1212_CPU_TO_BE32((CSR1212_DESCRIPTOR_LEAF_TYPE(kv) << \
  288. CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT) | \
  289. ((spec_id) & CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID_MASK)))
  290. /* Text Descriptor Leaf manipulation macros */
  291. #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_SHIFT 28
  292. #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_MASK 0xf /* after shift */
  293. #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_SHIFT 16
  294. #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_MASK 0xfff /* after shift */
  295. #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE_MASK 0xffff
  296. #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_OVERHEAD (1 * sizeof(u_int32_t))
  297. #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) \
  298. (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[1]) >> \
  299. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_SHIFT)
  300. #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) \
  301. ((CSR1212_BE32_TO_CPU((kv)->value.leaf.data[1]) >> \
  302. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_SHIFT) & \
  303. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_MASK)
  304. #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) \
  305. (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[1]) & \
  306. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE_MASK)
  307. #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(kv) \
  308. (&((kv)->value.leaf.data[2]))
  309. #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_SET_WIDTH(kv, width) \
  310. ((kv)->value.leaf.data[1] = \
  311. ((kv)->value.leaf.data[1] & \
  312. CSR1212_CPU_TO_BE32(~(CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_MASK << \
  313. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_SHIFT))) | \
  314. CSR1212_CPU_TO_BE32(((width) & \
  315. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_MASK) << \
  316. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_SHIFT))
  317. #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_SET_CHAR_SET(kv, char_set) \
  318. ((kv)->value.leaf.data[1] = \
  319. ((kv)->value.leaf.data[1] & \
  320. CSR1212_CPU_TO_BE32(~(CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_MASK << \
  321. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_SHIFT))) | \
  322. CSR1212_CPU_TO_BE32(((char_set) & \
  323. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_MASK) << \
  324. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_SHIFT))
  325. #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_SET_LANGUAGE(kv, language) \
  326. ((kv)->value.leaf.data[1] = \
  327. ((kv)->value.leaf.data[1] & \
  328. CSR1212_CPU_TO_BE32(~(CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE_MASK))) | \
  329. CSR1212_CPU_TO_BE32(((language) & \
  330. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE_MASK)))
  331. /* Icon Descriptor Leaf manipulation macros */
  332. #define CSR1212_ICON_DESCRIPTOR_LEAF_VERSION_MASK 0xffffff
  333. #define CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH_SHIFT 30
  334. #define CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH_MASK 0x3 /* after shift */
  335. #define CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_SHIFT 16
  336. #define CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_MASK 0xf /* after shift */
  337. #define CSR1212_ICON_DESCRIPTOR_LEAF_LANGUAGE_MASK 0xffff
  338. #define CSR1212_ICON_DESCRIPTOR_LEAF_HSCAN_SHIFT 16
  339. #define CSR1212_ICON_DESCRIPTOR_LEAF_HSCAN_MASK 0xffff /* after shift */
  340. #define CSR1212_ICON_DESCRIPTOR_LEAF_VSCAN_MASK 0xffff
  341. #define CSR1212_ICON_DESCRIPTOR_LEAF_OVERHEAD (3 * sizeof(u_int32_t))
  342. #define CSR1212_ICON_DESCRIPTOR_LEAF_VERSION(kv) \
  343. (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[2]) & \
  344. CSR1212_ICON_DESCRIPTOR_LEAF_VERSION_MASK)
  345. #define CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH(kv) \
  346. (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[3]) >> \
  347. CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH_SHIFT)
  348. #define CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE(kv) \
  349. ((CSR1212_BE32_TO_CPU((kv)->value.leaf.data[3]) >> \
  350. CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_SHIFT) & \
  351. CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_MASK)
  352. #define CSR1212_ICON_DESCRIPTOR_LEAF_LANGUAGE(kv) \
  353. (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[3]) & \
  354. CSR1212_ICON_DESCRIPTOR_LEAF_LANGUAGE_MASK)
  355. #define CSR1212_ICON_DESCRIPTOR_LEAF_HSCAN(kv) \
  356. ((CSR1212_BE32_TO_CPU((kv)->value.leaf.data[4]) >> \
  357. CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_HSCAN_SHIFT) & \
  358. CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_HSCAN_MASK)
  359. #define CSR1212_ICON_DESCRIPTOR_LEAF_VSCAN(kv) \
  360. (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[4]) & \
  361. CSR1212_ICON_DESCRIPTOR_LEAF_VSCAN_MASK)
  362. #define CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE(kv) \
  363. (&((kv)->value.leaf.data[5]))
  364. static inline u_int32_t *CSR1212_ICON_DESCRIPTOR_LEAF_PIXELS(struct csr1212_keyval *kv)
  365. {
  366. static const int pd[4] = { 0, 4, 16, 256 };
  367. static const int cs[16] = { 4, 2 };
  368. int ps = pd[CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH(kv)];
  369. return &kv->value.leaf.data[5 +
  370. (ps * cs[CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE(kv)]) /
  371. sizeof(u_int32_t)];
  372. }
  373. #define CSR1212_ICON_DESCRIPTOR_LEAF_SET_VERSION(kv, version) \
  374. ((kv)->value.leaf.data[2] = \
  375. ((kv)->value.leaf.data[2] & \
  376. CSR1212_CPU_TO_BE32(~(CSR1212_ICON_DESCRIPTOR_LEAF_VERSION_MASK))) | \
  377. CSR1212_CPU_TO_BE32(((version) & \
  378. CSR1212_ICON_DESCRIPTOR_LEAF_VERSION_MASK)))
  379. #define CSR1212_ICON_DESCRIPTOR_LEAF_SET_PALETTE_DEPTH(kv, palette_depth) \
  380. ((kv)->value.leaf.data[3] = \
  381. ((kv)->value.leaf.data[3] & \
  382. CSR1212_CPU_TO_BE32(~(CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH_MASK << \
  383. CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH_SHIFT))) | \
  384. CSR1212_CPU_TO_BE32(((palette_depth) & \
  385. CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH_MASK) << \
  386. CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH_SHIFT))
  387. #define CSR1212_ICON_DESCRIPTOR_LEAF_SET_COLOR_SPACE(kv, color_space) \
  388. ((kv)->value.leaf.data[3] = \
  389. ((kv)->value.leaf.data[3] & \
  390. CSR1212_CPU_TO_BE32(~(CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_MASK << \
  391. CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_SHIFT))) | \
  392. CSR1212_CPU_TO_BE32(((color_space) & \
  393. CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_MASK) << \
  394. CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_SHIFT))
  395. #define CSR1212_ICON_DESCRIPTOR_LEAF_SET_LANGUAGE(kv, language) \
  396. ((kv)->value.leaf.data[3] = \
  397. ((kv)->value.leaf.data[3] & \
  398. CSR1212_CPU_TO_BE32(~(CSR1212_ICON_DESCRIPTOR_LEAF_LANGUAGE_MASK))) | \
  399. CSR1212_CPU_TO_BE32(((language) & \
  400. CSR1212_ICON_DESCRIPTOR_LEAF_LANGUAGE_MASK)))
  401. #define CSR1212_ICON_DESCRIPTOR_LEAF_SET_HSCAN(kv, hscan) \
  402. ((kv)->value.leaf.data[4] = \
  403. ((kv)->value.leaf.data[4] & \
  404. CSR1212_CPU_TO_BE32(~(CSR1212_ICON_DESCRIPTOR_LEAF_HSCAN_MASK << \
  405. CSR1212_ICON_DESCRIPTOR_LEAF_HSCAN_SHIFT))) | \
  406. CSR1212_CPU_TO_BE32(((hscan) & \
  407. CSR1212_ICON_DESCRIPTOR_LEAF_HSCAN_MASK) << \
  408. CSR1212_ICON_DESCRIPTOR_LEAF_HSCAN_SHIFT))
  409. #define CSR1212_ICON_DESCRIPTOR_LEAF_SET_VSCAN(kv, vscan) \
  410. ((kv)->value.leaf.data[4] = \
  411. (((kv)->value.leaf.data[4] & \
  412. CSR1212_CPU_TO_BE32(~CSR1212_ICON_DESCRIPTOR_LEAF_VSCAN_MASK))) | \
  413. CSR1212_CPU_TO_BE32(((vscan) & \
  414. CSR1212_ICON_DESCRIPTOR_LEAF_VSCAN_MASK)))
  415. /* Modifiable Descriptor Leaf manipulation macros */
  416. #define CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_MAX_SIZE_SHIFT 16
  417. #define CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_MAX_SIZE_MASK 0xffff
  418. #define CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_ADDR_HI_SHIFT 32
  419. #define CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_ADDR_HI_MASK 0xffff
  420. #define CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_ADDR_LO_MASK 0xffffffffULL
  421. #define CSR1212_MODIFIABLE_DESCRIPTOR_MAX_SIZE(kv) \
  422. CSR1212_BE16_TO_CPU((kv)->value.leaf.data[0] >> CSR1212_MODIFIABLE_DESCRIPTOR_MAX_SIZE_SHIFT)
  423. #define CSR1212_MODIFIABLE_DESCRIPTOR_ADDRESS(kv) \
  424. (CSR1212_BE16_TO_CPU(((u_int64_t)((kv)->value.leaf.data[0])) << \
  425. CSR1212_MODIFIABLE_DESCRIPTOR_ADDR_HI_SHIFT) | \
  426. CSR1212_BE32_TO_CPU((kv)->value.leaf.data[1]))
  427. #define CSR1212_MODIFIABLE_DESCRIPTOR_SET_MAX_SIZE(kv, size) \
  428. ((kv)->value.leaf.data[0] = \
  429. ((kv)->value.leaf.data[0] & \
  430. CSR1212_CPU_TO_BE32(~(CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_MAX_SIZE_MASK << \
  431. CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_MAX_SIZE_SHIFT))) | \
  432. CSR1212_CPU_TO_BE32(((size) & \
  433. CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_MAX_SIZE_MASK) << \
  434. CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_MAX_SIZE_SHIFT))
  435. #define CSR1212_MODIFIABLE_DESCRIPTOR_SET_ADDRESS_HI(kv, addr) \
  436. ((kv)->value.leaf.data[0] = \
  437. ((kv)->value.leaf.data[0] & \
  438. CSR1212_CPU_TO_BE32(~(CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_ADDR_HI_MASK))) | \
  439. CSR1212_CPU_TO_BE32(((addr) & \
  440. CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_ADDR_HI_MASK)))
  441. #define CSR1212_MODIFIABLE_DESCRIPTOR_SET_ADDRESS_LO(kv, addr) \
  442. ((kv)->value.leaf.data[1] = \
  443. CSR1212_CPU_TO_BE32(addr & CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_ADDR_LO_MASK))
  444. /* The following 2 function are for creating new Configuration ROM trees. The
  445. * first function is used for both creating local trees and parsing remote
  446. * trees. The second function adds pertinent information to local Configuration
  447. * ROM trees - namely data for the bus information block. */
  448. extern struct csr1212_csr *csr1212_create_csr(struct csr1212_bus_ops *ops,
  449. size_t bus_info_size,
  450. void *private);
  451. extern void csr1212_init_local_csr(struct csr1212_csr *csr,
  452. const u_int32_t *bus_info_data, int max_rom);
  453. /* The following function destroys a Configuration ROM tree and release all
  454. * memory taken by the tree. */
  455. extern void csr1212_destroy_csr(struct csr1212_csr *csr);
  456. /* The following set of functions are fore creating new keyvals for placement in
  457. * a Configuration ROM tree. Code that creates new keyvals with these functions
  458. * must release those keyvals with csr1212_release_keyval() when they are no
  459. * longer needed. */
  460. extern struct csr1212_keyval *csr1212_new_immediate(u_int8_t key, u_int32_t value);
  461. extern struct csr1212_keyval *csr1212_new_leaf(u_int8_t key, const void *data,
  462. size_t data_len);
  463. extern struct csr1212_keyval *csr1212_new_csr_offset(u_int8_t key,
  464. u_int32_t csr_offset);
  465. extern struct csr1212_keyval *csr1212_new_directory(u_int8_t key);
  466. extern struct csr1212_keyval *csr1212_new_extended_immediate(u_int32_t spec,
  467. u_int32_t key,
  468. u_int32_t value);
  469. extern struct csr1212_keyval *csr1212_new_extended_leaf(u_int32_t spec,
  470. u_int32_t key,
  471. const void *data,
  472. size_t data_len);
  473. extern struct csr1212_keyval *csr1212_new_descriptor_leaf(u_int8_t dtype,
  474. u_int32_t specifier_id,
  475. const void *data,
  476. size_t data_len);
  477. extern struct csr1212_keyval *csr1212_new_textual_descriptor_leaf(u_int8_t cwidth,
  478. u_int16_t cset,
  479. u_int16_t language,
  480. const void *data,
  481. size_t data_len);
  482. extern struct csr1212_keyval *csr1212_new_string_descriptor_leaf(const char *s);
  483. extern struct csr1212_keyval *csr1212_new_icon_descriptor_leaf(u_int32_t version,
  484. u_int8_t palette_depth,
  485. u_int8_t color_space,
  486. u_int16_t language,
  487. u_int16_t hscan,
  488. u_int16_t vscan,
  489. u_int32_t *palette,
  490. u_int32_t *pixels);
  491. extern struct csr1212_keyval *csr1212_new_modifiable_descriptor_leaf(u_int16_t max_size,
  492. u_int64_t address);
  493. extern struct csr1212_keyval *csr1212_new_keyword_leaf(int strc,
  494. const char *strv[]);
  495. /* The following functions manage association between keyvals. Typically,
  496. * Descriptor Leaves and Directories will be associated with another keyval and
  497. * it is desirable for the Descriptor keyval to be place immediately after the
  498. * keyval that it is associated with.*/
  499. extern int csr1212_associate_keyval(struct csr1212_keyval *kv,
  500. struct csr1212_keyval *associate);
  501. extern void csr1212_disassociate_keyval(struct csr1212_keyval *kv);
  502. /* The following functions manage the association of a keyval and directories.
  503. * A keyval may be attached to more than one directory. */
  504. extern int csr1212_attach_keyval_to_directory(struct csr1212_keyval *dir,
  505. struct csr1212_keyval *kv);
  506. extern void csr1212_detach_keyval_from_directory(struct csr1212_keyval *dir,
  507. struct csr1212_keyval *kv);
  508. /* The following functions create a Configuration ROM image from the tree of
  509. * keyvals provided. csr1212_generate_csr_image() creates a complete image in
  510. * the list of caches available via csr->cache_head. The other functions are
  511. * provided should there be a need to create a flat image without restrictions
  512. * placed by IEEE 1212. */
  513. extern struct csr1212_keyval *csr1212_generate_positions(struct csr1212_csr_rom_cache *cache,
  514. struct csr1212_keyval *start_kv,
  515. int start_pos);
  516. extern size_t csr1212_generate_layout_order(struct csr1212_keyval *kv);
  517. extern void csr1212_fill_cache(struct csr1212_csr_rom_cache *cache);
  518. extern int csr1212_generate_csr_image(struct csr1212_csr *csr);
  519. /* This is a convience function for reading a block of data out of one of the
  520. * caches in the csr->cache_head list. */
  521. extern int csr1212_read(struct csr1212_csr *csr, u_int32_t offset, void *buffer,
  522. u_int32_t len);
  523. /* The following functions are in place for parsing Configuration ROM images.
  524. * csr1212_parse_keyval() is used should there be a need to directly parse a
  525. * Configuration ROM directly. */
  526. extern int csr1212_parse_keyval(struct csr1212_keyval *kv,
  527. struct csr1212_csr_rom_cache *cache);
  528. extern int csr1212_parse_csr(struct csr1212_csr *csr);
  529. /* These are internal functions referenced by inline functions below. */
  530. extern int _csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv);
  531. extern void _csr1212_destroy_keyval(struct csr1212_keyval *kv);
  532. /* This function allocates a new cache which may be used for either parsing or
  533. * generating sub-sets of Configuration ROM images. */
  534. static inline struct csr1212_csr_rom_cache *csr1212_rom_cache_malloc(u_int32_t offset,
  535. size_t size)
  536. {
  537. struct csr1212_csr_rom_cache *cache;
  538. cache = CSR1212_MALLOC(sizeof(*cache) + size);
  539. if (!cache)
  540. return NULL;
  541. cache->next = NULL;
  542. cache->prev = NULL;
  543. cache->filled_head = NULL;
  544. cache->filled_tail = NULL;
  545. cache->layout_head = NULL;
  546. cache->layout_tail = NULL;
  547. cache->offset = offset;
  548. cache->size = size;
  549. cache->ext_rom = NULL;
  550. return cache;
  551. }
  552. /* This function ensures that a keyval contains data when referencing a keyval
  553. * created by parsing a Configuration ROM. */
  554. static inline struct csr1212_keyval *csr1212_get_keyval(struct csr1212_csr *csr,
  555. struct csr1212_keyval *kv)
  556. {
  557. if (!kv)
  558. return NULL;
  559. if (!kv->valid)
  560. if (_csr1212_read_keyval(csr, kv) != CSR1212_SUCCESS)
  561. return NULL;
  562. return kv;
  563. }
  564. /* This function increments the reference count for a keyval should there be a
  565. * need for code to retain a keyval that has been parsed. */
  566. static inline void csr1212_keep_keyval(struct csr1212_keyval *kv)
  567. {
  568. kv->refcnt++;
  569. }
  570. /* This function decrements a keyval's reference count and will destroy the
  571. * keyval when there are no more users of the keyval. This should be called by
  572. * any code that calls csr1212_keep_keyval() or any of the keyval creation
  573. * routines csr1212_new_*(). */
  574. static inline void csr1212_release_keyval(struct csr1212_keyval *kv)
  575. {
  576. if (kv->refcnt > 1)
  577. kv->refcnt--;
  578. else
  579. _csr1212_destroy_keyval(kv);
  580. }
  581. /*
  582. * This macro allows for looping over the keyval entries in a directory and it
  583. * ensures that keyvals from remote ConfigROMs are parsed properly.
  584. *
  585. * _csr is a struct csr1212_csr * that points to CSR associated with dir.
  586. * _kv is a struct csr1212_keyval * that'll point to the current keyval (loop index).
  587. * _dir is a struct csr1212_keyval * that points to the directory to be looped.
  588. * _pos is a struct csr1212_dentry * that is used internally for indexing.
  589. *
  590. * kv will be NULL upon exit of the loop.
  591. */
  592. #define csr1212_for_each_dir_entry(_csr, _kv, _dir, _pos) \
  593. for (csr1212_get_keyval((_csr), (_dir)), \
  594. _pos = (_dir)->value.directory.dentries_head, \
  595. _kv = (_pos) ? csr1212_get_keyval((_csr), _pos->kv) : NULL; \
  596. (_kv) && (_pos); \
  597. (_kv->associate == NULL) ? \
  598. ((_pos = _pos->next), \
  599. (_kv = (_pos) ? csr1212_get_keyval((_csr), _pos->kv) : \
  600. NULL)) : \
  601. (_kv = csr1212_get_keyval((_csr), _kv->associate)))
  602. #endif /* __CSR1212_H__ */