cxl-host.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * CXL host parameter parsing routines
  3. *
  4. * Copyright (c) 2022 Huawei
  5. * Modeled loosely on the NUMA options handling in hw/core/numa.c
  6. */
  7. #include "qemu/osdep.h"
  8. #include "qemu/units.h"
  9. #include "qemu/bitmap.h"
  10. #include "qemu/error-report.h"
  11. #include "qapi/error.h"
  12. #include "sysemu/qtest.h"
  13. #include "hw/boards.h"
  14. #include "qapi/qapi-visit-machine.h"
  15. #include "hw/cxl/cxl.h"
  16. #include "hw/cxl/cxl_host.h"
  17. #include "hw/pci/pci_bus.h"
  18. #include "hw/pci/pci_bridge.h"
  19. #include "hw/pci/pci_host.h"
  20. #include "hw/pci/pcie_port.h"
  21. #include "hw/pci-bridge/pci_expander_bridge.h"
  22. static void cxl_fixed_memory_window_config(CXLState *cxl_state,
  23. CXLFixedMemoryWindowOptions *object,
  24. Error **errp)
  25. {
  26. g_autofree CXLFixedWindow *fw = g_malloc0(sizeof(*fw));
  27. strList *target;
  28. int i;
  29. for (target = object->targets; target; target = target->next) {
  30. fw->num_targets++;
  31. }
  32. fw->enc_int_ways = cxl_interleave_ways_enc(fw->num_targets, errp);
  33. if (*errp) {
  34. return;
  35. }
  36. fw->targets = g_malloc0_n(fw->num_targets, sizeof(*fw->targets));
  37. for (i = 0, target = object->targets; target; i++, target = target->next) {
  38. /* This link cannot be resolved yet, so stash the name for now */
  39. fw->targets[i] = g_strdup(target->value);
  40. }
  41. if (object->size % (256 * MiB)) {
  42. error_setg(errp,
  43. "Size of a CXL fixed memory window must be a multiple of 256MiB");
  44. return;
  45. }
  46. fw->size = object->size;
  47. if (object->has_interleave_granularity) {
  48. fw->enc_int_gran =
  49. cxl_interleave_granularity_enc(object->interleave_granularity,
  50. errp);
  51. if (*errp) {
  52. return;
  53. }
  54. } else {
  55. /* Default to 256 byte interleave */
  56. fw->enc_int_gran = 0;
  57. }
  58. cxl_state->fixed_windows = g_list_append(cxl_state->fixed_windows,
  59. g_steal_pointer(&fw));
  60. return;
  61. }
  62. void cxl_fmws_link_targets(CXLState *cxl_state, Error **errp)
  63. {
  64. if (cxl_state && cxl_state->fixed_windows) {
  65. GList *it;
  66. for (it = cxl_state->fixed_windows; it; it = it->next) {
  67. CXLFixedWindow *fw = it->data;
  68. int i;
  69. for (i = 0; i < fw->num_targets; i++) {
  70. Object *o;
  71. bool ambig;
  72. o = object_resolve_path_type(fw->targets[i],
  73. TYPE_PXB_CXL_DEVICE,
  74. &ambig);
  75. if (!o) {
  76. error_setg(errp, "Could not resolve CXLFM target %s",
  77. fw->targets[i]);
  78. return;
  79. }
  80. fw->target_hbs[i] = PXB_CXL_DEV(o);
  81. }
  82. }
  83. }
  84. }
  85. /* TODO: support, multiple hdm decoders */
  86. static bool cxl_hdm_find_target(uint32_t *cache_mem, hwaddr addr,
  87. uint8_t *target)
  88. {
  89. uint32_t ctrl;
  90. uint32_t ig_enc;
  91. uint32_t iw_enc;
  92. uint32_t target_idx;
  93. ctrl = cache_mem[R_CXL_HDM_DECODER0_CTRL];
  94. if (!FIELD_EX32(ctrl, CXL_HDM_DECODER0_CTRL, COMMITTED)) {
  95. return false;
  96. }
  97. ig_enc = FIELD_EX32(ctrl, CXL_HDM_DECODER0_CTRL, IG);
  98. iw_enc = FIELD_EX32(ctrl, CXL_HDM_DECODER0_CTRL, IW);
  99. target_idx = (addr / cxl_decode_ig(ig_enc)) % (1 << iw_enc);
  100. if (target_idx < 4) {
  101. *target = extract32(cache_mem[R_CXL_HDM_DECODER0_TARGET_LIST_LO],
  102. target_idx * 8, 8);
  103. } else {
  104. *target = extract32(cache_mem[R_CXL_HDM_DECODER0_TARGET_LIST_HI],
  105. (target_idx - 4) * 8, 8);
  106. }
  107. return true;
  108. }
  109. static PCIDevice *cxl_cfmws_find_device(CXLFixedWindow *fw, hwaddr addr)
  110. {
  111. CXLComponentState *hb_cstate, *usp_cstate;
  112. PCIHostState *hb;
  113. CXLUpstreamPort *usp;
  114. int rb_index;
  115. uint32_t *cache_mem;
  116. uint8_t target;
  117. bool target_found;
  118. PCIDevice *rp, *d;
  119. /* Address is relative to memory region. Convert to HPA */
  120. addr += fw->base;
  121. rb_index = (addr / cxl_decode_ig(fw->enc_int_gran)) % fw->num_targets;
  122. hb = PCI_HOST_BRIDGE(fw->target_hbs[rb_index]->cxl.cxl_host_bridge);
  123. if (!hb || !hb->bus || !pci_bus_is_cxl(hb->bus)) {
  124. return NULL;
  125. }
  126. if (cxl_get_hb_passthrough(hb)) {
  127. rp = pcie_find_port_first(hb->bus);
  128. if (!rp) {
  129. return NULL;
  130. }
  131. } else {
  132. hb_cstate = cxl_get_hb_cstate(hb);
  133. if (!hb_cstate) {
  134. return NULL;
  135. }
  136. cache_mem = hb_cstate->crb.cache_mem_registers;
  137. target_found = cxl_hdm_find_target(cache_mem, addr, &target);
  138. if (!target_found) {
  139. return NULL;
  140. }
  141. rp = pcie_find_port_by_pn(hb->bus, target);
  142. if (!rp) {
  143. return NULL;
  144. }
  145. }
  146. d = pci_bridge_get_sec_bus(PCI_BRIDGE(rp))->devices[0];
  147. if (!d) {
  148. return NULL;
  149. }
  150. if (object_dynamic_cast(OBJECT(d), TYPE_CXL_TYPE3)) {
  151. return d;
  152. }
  153. /*
  154. * Could also be a switch. Note only one level of switching currently
  155. * supported.
  156. */
  157. if (!object_dynamic_cast(OBJECT(d), TYPE_CXL_USP)) {
  158. return NULL;
  159. }
  160. usp = CXL_USP(d);
  161. usp_cstate = cxl_usp_to_cstate(usp);
  162. if (!usp_cstate) {
  163. return NULL;
  164. }
  165. cache_mem = usp_cstate->crb.cache_mem_registers;
  166. target_found = cxl_hdm_find_target(cache_mem, addr, &target);
  167. if (!target_found) {
  168. return NULL;
  169. }
  170. d = pcie_find_port_by_pn(&PCI_BRIDGE(d)->sec_bus, target);
  171. if (!d) {
  172. return NULL;
  173. }
  174. d = pci_bridge_get_sec_bus(PCI_BRIDGE(d))->devices[0];
  175. if (!d) {
  176. return NULL;
  177. }
  178. if (!object_dynamic_cast(OBJECT(d), TYPE_CXL_TYPE3)) {
  179. return NULL;
  180. }
  181. return d;
  182. }
  183. static MemTxResult cxl_read_cfmws(void *opaque, hwaddr addr, uint64_t *data,
  184. unsigned size, MemTxAttrs attrs)
  185. {
  186. CXLFixedWindow *fw = opaque;
  187. PCIDevice *d;
  188. d = cxl_cfmws_find_device(fw, addr);
  189. if (d == NULL) {
  190. *data = 0;
  191. /* Reads to invalid address return poison */
  192. return MEMTX_ERROR;
  193. }
  194. return cxl_type3_read(d, addr + fw->base, data, size, attrs);
  195. }
  196. static MemTxResult cxl_write_cfmws(void *opaque, hwaddr addr,
  197. uint64_t data, unsigned size,
  198. MemTxAttrs attrs)
  199. {
  200. CXLFixedWindow *fw = opaque;
  201. PCIDevice *d;
  202. d = cxl_cfmws_find_device(fw, addr);
  203. if (d == NULL) {
  204. /* Writes to invalid address are silent */
  205. return MEMTX_OK;
  206. }
  207. return cxl_type3_write(d, addr + fw->base, data, size, attrs);
  208. }
  209. const MemoryRegionOps cfmws_ops = {
  210. .read_with_attrs = cxl_read_cfmws,
  211. .write_with_attrs = cxl_write_cfmws,
  212. .endianness = DEVICE_LITTLE_ENDIAN,
  213. .valid = {
  214. .min_access_size = 1,
  215. .max_access_size = 8,
  216. .unaligned = true,
  217. },
  218. .impl = {
  219. .min_access_size = 1,
  220. .max_access_size = 8,
  221. .unaligned = true,
  222. },
  223. };
  224. static void machine_get_cxl(Object *obj, Visitor *v, const char *name,
  225. void *opaque, Error **errp)
  226. {
  227. CXLState *cxl_state = opaque;
  228. bool value = cxl_state->is_enabled;
  229. visit_type_bool(v, name, &value, errp);
  230. }
  231. static void machine_set_cxl(Object *obj, Visitor *v, const char *name,
  232. void *opaque, Error **errp)
  233. {
  234. CXLState *cxl_state = opaque;
  235. bool value;
  236. if (!visit_type_bool(v, name, &value, errp)) {
  237. return;
  238. }
  239. cxl_state->is_enabled = value;
  240. }
  241. static void machine_get_cfmw(Object *obj, Visitor *v, const char *name,
  242. void *opaque, Error **errp)
  243. {
  244. CXLFixedMemoryWindowOptionsList **list = opaque;
  245. visit_type_CXLFixedMemoryWindowOptionsList(v, name, list, errp);
  246. }
  247. static void machine_set_cfmw(Object *obj, Visitor *v, const char *name,
  248. void *opaque, Error **errp)
  249. {
  250. CXLState *state = opaque;
  251. CXLFixedMemoryWindowOptionsList *cfmw_list = NULL;
  252. CXLFixedMemoryWindowOptionsList *it;
  253. visit_type_CXLFixedMemoryWindowOptionsList(v, name, &cfmw_list, errp);
  254. if (!cfmw_list) {
  255. return;
  256. }
  257. for (it = cfmw_list; it; it = it->next) {
  258. cxl_fixed_memory_window_config(state, it->value, errp);
  259. }
  260. state->cfmw_list = cfmw_list;
  261. }
  262. void cxl_machine_init(Object *obj, CXLState *state)
  263. {
  264. object_property_add(obj, "cxl", "bool", machine_get_cxl,
  265. machine_set_cxl, NULL, state);
  266. object_property_set_description(obj, "cxl",
  267. "Set on/off to enable/disable "
  268. "CXL instantiation");
  269. object_property_add(obj, "cxl-fmw", "CXLFixedMemoryWindow",
  270. machine_get_cfmw, machine_set_cfmw,
  271. NULL, state);
  272. object_property_set_description(obj, "cxl-fmw",
  273. "CXL Fixed Memory Windows (array)");
  274. }
  275. void cxl_hook_up_pxb_registers(PCIBus *bus, CXLState *state, Error **errp)
  276. {
  277. /* Walk the pci busses looking for pxb busses to hook up */
  278. if (bus) {
  279. QLIST_FOREACH(bus, &bus->child, sibling) {
  280. if (!pci_bus_is_root(bus)) {
  281. continue;
  282. }
  283. if (pci_bus_is_cxl(bus)) {
  284. if (!state->is_enabled) {
  285. error_setg(errp, "CXL host bridges present, but cxl=off");
  286. return;
  287. }
  288. pxb_cxl_hook_up_registers(state, bus, errp);
  289. }
  290. }
  291. }
  292. }