cxl-host.c 11 KB

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