guest.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. /*
  2. * Copyright 2015 IBM Corp.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/spinlock.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/delay.h>
  12. #include "cxl.h"
  13. #include "hcalls.h"
  14. #include "trace.h"
  15. #define CXL_ERROR_DETECTED_EVENT 1
  16. #define CXL_SLOT_RESET_EVENT 2
  17. #define CXL_RESUME_EVENT 3
  18. static void pci_error_handlers(struct cxl_afu *afu,
  19. int bus_error_event,
  20. pci_channel_state_t state)
  21. {
  22. struct pci_dev *afu_dev;
  23. if (afu->phb == NULL)
  24. return;
  25. list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
  26. if (!afu_dev->driver)
  27. continue;
  28. switch (bus_error_event) {
  29. case CXL_ERROR_DETECTED_EVENT:
  30. afu_dev->error_state = state;
  31. if (afu_dev->driver->err_handler &&
  32. afu_dev->driver->err_handler->error_detected)
  33. afu_dev->driver->err_handler->error_detected(afu_dev, state);
  34. break;
  35. case CXL_SLOT_RESET_EVENT:
  36. afu_dev->error_state = state;
  37. if (afu_dev->driver->err_handler &&
  38. afu_dev->driver->err_handler->slot_reset)
  39. afu_dev->driver->err_handler->slot_reset(afu_dev);
  40. break;
  41. case CXL_RESUME_EVENT:
  42. if (afu_dev->driver->err_handler &&
  43. afu_dev->driver->err_handler->resume)
  44. afu_dev->driver->err_handler->resume(afu_dev);
  45. break;
  46. }
  47. }
  48. }
  49. static irqreturn_t guest_handle_psl_slice_error(struct cxl_context *ctx, u64 dsisr,
  50. u64 errstat)
  51. {
  52. pr_devel("in %s\n", __func__);
  53. dev_crit(&ctx->afu->dev, "PSL ERROR STATUS: 0x%.16llx\n", errstat);
  54. return cxl_ops->ack_irq(ctx, 0, errstat);
  55. }
  56. static ssize_t guest_collect_vpd(struct cxl *adapter, struct cxl_afu *afu,
  57. void *buf, size_t len)
  58. {
  59. unsigned int entries, mod;
  60. unsigned long **vpd_buf = NULL;
  61. struct sg_list *le;
  62. int rc = 0, i, tocopy;
  63. u64 out = 0;
  64. if (buf == NULL)
  65. return -EINVAL;
  66. /* number of entries in the list */
  67. entries = len / SG_BUFFER_SIZE;
  68. mod = len % SG_BUFFER_SIZE;
  69. if (mod)
  70. entries++;
  71. if (entries > SG_MAX_ENTRIES) {
  72. entries = SG_MAX_ENTRIES;
  73. len = SG_MAX_ENTRIES * SG_BUFFER_SIZE;
  74. mod = 0;
  75. }
  76. vpd_buf = kcalloc(entries, sizeof(unsigned long *), GFP_KERNEL);
  77. if (!vpd_buf)
  78. return -ENOMEM;
  79. le = (struct sg_list *)get_zeroed_page(GFP_KERNEL);
  80. if (!le) {
  81. rc = -ENOMEM;
  82. goto err1;
  83. }
  84. for (i = 0; i < entries; i++) {
  85. vpd_buf[i] = (unsigned long *)get_zeroed_page(GFP_KERNEL);
  86. if (!vpd_buf[i]) {
  87. rc = -ENOMEM;
  88. goto err2;
  89. }
  90. le[i].phys_addr = cpu_to_be64(virt_to_phys(vpd_buf[i]));
  91. le[i].len = cpu_to_be64(SG_BUFFER_SIZE);
  92. if ((i == (entries - 1)) && mod)
  93. le[i].len = cpu_to_be64(mod);
  94. }
  95. if (adapter)
  96. rc = cxl_h_collect_vpd_adapter(adapter->guest->handle,
  97. virt_to_phys(le), entries, &out);
  98. else
  99. rc = cxl_h_collect_vpd(afu->guest->handle, 0,
  100. virt_to_phys(le), entries, &out);
  101. pr_devel("length of available (entries: %i), vpd: %#llx\n",
  102. entries, out);
  103. if (!rc) {
  104. /*
  105. * hcall returns in 'out' the size of available VPDs.
  106. * It fills the buffer with as much data as possible.
  107. */
  108. if (out < len)
  109. len = out;
  110. rc = len;
  111. if (out) {
  112. for (i = 0; i < entries; i++) {
  113. if (len < SG_BUFFER_SIZE)
  114. tocopy = len;
  115. else
  116. tocopy = SG_BUFFER_SIZE;
  117. memcpy(buf, vpd_buf[i], tocopy);
  118. buf += tocopy;
  119. len -= tocopy;
  120. }
  121. }
  122. }
  123. err2:
  124. for (i = 0; i < entries; i++) {
  125. if (vpd_buf[i])
  126. free_page((unsigned long) vpd_buf[i]);
  127. }
  128. free_page((unsigned long) le);
  129. err1:
  130. kfree(vpd_buf);
  131. return rc;
  132. }
  133. static int guest_get_irq_info(struct cxl_context *ctx, struct cxl_irq_info *info)
  134. {
  135. return cxl_h_collect_int_info(ctx->afu->guest->handle, ctx->process_token, info);
  136. }
  137. static irqreturn_t guest_psl_irq(int irq, void *data)
  138. {
  139. struct cxl_context *ctx = data;
  140. struct cxl_irq_info irq_info;
  141. int rc;
  142. pr_devel("%d: received PSL interrupt %i\n", ctx->pe, irq);
  143. rc = guest_get_irq_info(ctx, &irq_info);
  144. if (rc) {
  145. WARN(1, "Unable to get IRQ info: %i\n", rc);
  146. return IRQ_HANDLED;
  147. }
  148. rc = cxl_irq_psl8(irq, ctx, &irq_info);
  149. return rc;
  150. }
  151. static int afu_read_error_state(struct cxl_afu *afu, int *state_out)
  152. {
  153. u64 state;
  154. int rc = 0;
  155. if (!afu)
  156. return -EIO;
  157. rc = cxl_h_read_error_state(afu->guest->handle, &state);
  158. if (!rc) {
  159. WARN_ON(state != H_STATE_NORMAL &&
  160. state != H_STATE_DISABLE &&
  161. state != H_STATE_TEMP_UNAVAILABLE &&
  162. state != H_STATE_PERM_UNAVAILABLE);
  163. *state_out = state & 0xffffffff;
  164. }
  165. return rc;
  166. }
  167. static irqreturn_t guest_slice_irq_err(int irq, void *data)
  168. {
  169. struct cxl_afu *afu = data;
  170. int rc;
  171. u64 serr, afu_error, dsisr;
  172. rc = cxl_h_get_fn_error_interrupt(afu->guest->handle, &serr);
  173. if (rc) {
  174. dev_crit(&afu->dev, "Couldn't read PSL_SERR_An: %d\n", rc);
  175. return IRQ_HANDLED;
  176. }
  177. afu_error = cxl_p2n_read(afu, CXL_AFU_ERR_An);
  178. dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
  179. cxl_afu_decode_psl_serr(afu, serr);
  180. dev_crit(&afu->dev, "AFU_ERR_An: 0x%.16llx\n", afu_error);
  181. dev_crit(&afu->dev, "PSL_DSISR_An: 0x%.16llx\n", dsisr);
  182. rc = cxl_h_ack_fn_error_interrupt(afu->guest->handle, serr);
  183. if (rc)
  184. dev_crit(&afu->dev, "Couldn't ack slice error interrupt: %d\n",
  185. rc);
  186. return IRQ_HANDLED;
  187. }
  188. static int irq_alloc_range(struct cxl *adapter, int len, int *irq)
  189. {
  190. int i, n;
  191. struct irq_avail *cur;
  192. for (i = 0; i < adapter->guest->irq_nranges; i++) {
  193. cur = &adapter->guest->irq_avail[i];
  194. n = bitmap_find_next_zero_area(cur->bitmap, cur->range,
  195. 0, len, 0);
  196. if (n < cur->range) {
  197. bitmap_set(cur->bitmap, n, len);
  198. *irq = cur->offset + n;
  199. pr_devel("guest: allocate IRQs %#x->%#x\n",
  200. *irq, *irq + len - 1);
  201. return 0;
  202. }
  203. }
  204. return -ENOSPC;
  205. }
  206. static int irq_free_range(struct cxl *adapter, int irq, int len)
  207. {
  208. int i, n;
  209. struct irq_avail *cur;
  210. if (len == 0)
  211. return -ENOENT;
  212. for (i = 0; i < adapter->guest->irq_nranges; i++) {
  213. cur = &adapter->guest->irq_avail[i];
  214. if (irq >= cur->offset &&
  215. (irq + len) <= (cur->offset + cur->range)) {
  216. n = irq - cur->offset;
  217. bitmap_clear(cur->bitmap, n, len);
  218. pr_devel("guest: release IRQs %#x->%#x\n",
  219. irq, irq + len - 1);
  220. return 0;
  221. }
  222. }
  223. return -ENOENT;
  224. }
  225. static int guest_reset(struct cxl *adapter)
  226. {
  227. struct cxl_afu *afu = NULL;
  228. int i, rc;
  229. pr_devel("Adapter reset request\n");
  230. for (i = 0; i < adapter->slices; i++) {
  231. if ((afu = adapter->afu[i])) {
  232. pci_error_handlers(afu, CXL_ERROR_DETECTED_EVENT,
  233. pci_channel_io_frozen);
  234. cxl_context_detach_all(afu);
  235. }
  236. }
  237. rc = cxl_h_reset_adapter(adapter->guest->handle);
  238. for (i = 0; i < adapter->slices; i++) {
  239. if (!rc && (afu = adapter->afu[i])) {
  240. pci_error_handlers(afu, CXL_SLOT_RESET_EVENT,
  241. pci_channel_io_normal);
  242. pci_error_handlers(afu, CXL_RESUME_EVENT, 0);
  243. }
  244. }
  245. return rc;
  246. }
  247. static int guest_alloc_one_irq(struct cxl *adapter)
  248. {
  249. int irq;
  250. spin_lock(&adapter->guest->irq_alloc_lock);
  251. if (irq_alloc_range(adapter, 1, &irq))
  252. irq = -ENOSPC;
  253. spin_unlock(&adapter->guest->irq_alloc_lock);
  254. return irq;
  255. }
  256. static void guest_release_one_irq(struct cxl *adapter, int irq)
  257. {
  258. spin_lock(&adapter->guest->irq_alloc_lock);
  259. irq_free_range(adapter, irq, 1);
  260. spin_unlock(&adapter->guest->irq_alloc_lock);
  261. }
  262. static int guest_alloc_irq_ranges(struct cxl_irq_ranges *irqs,
  263. struct cxl *adapter, unsigned int num)
  264. {
  265. int i, try, irq;
  266. memset(irqs, 0, sizeof(struct cxl_irq_ranges));
  267. spin_lock(&adapter->guest->irq_alloc_lock);
  268. for (i = 0; i < CXL_IRQ_RANGES && num; i++) {
  269. try = num;
  270. while (try) {
  271. if (irq_alloc_range(adapter, try, &irq) == 0)
  272. break;
  273. try /= 2;
  274. }
  275. if (!try)
  276. goto error;
  277. irqs->offset[i] = irq;
  278. irqs->range[i] = try;
  279. num -= try;
  280. }
  281. if (num)
  282. goto error;
  283. spin_unlock(&adapter->guest->irq_alloc_lock);
  284. return 0;
  285. error:
  286. for (i = 0; i < CXL_IRQ_RANGES; i++)
  287. irq_free_range(adapter, irqs->offset[i], irqs->range[i]);
  288. spin_unlock(&adapter->guest->irq_alloc_lock);
  289. return -ENOSPC;
  290. }
  291. static void guest_release_irq_ranges(struct cxl_irq_ranges *irqs,
  292. struct cxl *adapter)
  293. {
  294. int i;
  295. spin_lock(&adapter->guest->irq_alloc_lock);
  296. for (i = 0; i < CXL_IRQ_RANGES; i++)
  297. irq_free_range(adapter, irqs->offset[i], irqs->range[i]);
  298. spin_unlock(&adapter->guest->irq_alloc_lock);
  299. }
  300. static int guest_register_serr_irq(struct cxl_afu *afu)
  301. {
  302. afu->err_irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
  303. dev_name(&afu->dev));
  304. if (!afu->err_irq_name)
  305. return -ENOMEM;
  306. if (!(afu->serr_virq = cxl_map_irq(afu->adapter, afu->serr_hwirq,
  307. guest_slice_irq_err, afu, afu->err_irq_name))) {
  308. kfree(afu->err_irq_name);
  309. afu->err_irq_name = NULL;
  310. return -ENOMEM;
  311. }
  312. return 0;
  313. }
  314. static void guest_release_serr_irq(struct cxl_afu *afu)
  315. {
  316. cxl_unmap_irq(afu->serr_virq, afu);
  317. cxl_ops->release_one_irq(afu->adapter, afu->serr_hwirq);
  318. kfree(afu->err_irq_name);
  319. }
  320. static int guest_ack_irq(struct cxl_context *ctx, u64 tfc, u64 psl_reset_mask)
  321. {
  322. return cxl_h_control_faults(ctx->afu->guest->handle, ctx->process_token,
  323. tfc >> 32, (psl_reset_mask != 0));
  324. }
  325. static void disable_afu_irqs(struct cxl_context *ctx)
  326. {
  327. irq_hw_number_t hwirq;
  328. unsigned int virq;
  329. int r, i;
  330. pr_devel("Disabling AFU(%d) interrupts\n", ctx->afu->slice);
  331. for (r = 0; r < CXL_IRQ_RANGES; r++) {
  332. hwirq = ctx->irqs.offset[r];
  333. for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
  334. virq = irq_find_mapping(NULL, hwirq);
  335. disable_irq(virq);
  336. }
  337. }
  338. }
  339. static void enable_afu_irqs(struct cxl_context *ctx)
  340. {
  341. irq_hw_number_t hwirq;
  342. unsigned int virq;
  343. int r, i;
  344. pr_devel("Enabling AFU(%d) interrupts\n", ctx->afu->slice);
  345. for (r = 0; r < CXL_IRQ_RANGES; r++) {
  346. hwirq = ctx->irqs.offset[r];
  347. for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
  348. virq = irq_find_mapping(NULL, hwirq);
  349. enable_irq(virq);
  350. }
  351. }
  352. }
  353. static int _guest_afu_cr_readXX(int sz, struct cxl_afu *afu, int cr_idx,
  354. u64 offset, u64 *val)
  355. {
  356. unsigned long cr;
  357. char c;
  358. int rc = 0;
  359. if (afu->crs_len < sz)
  360. return -ENOENT;
  361. if (unlikely(offset >= afu->crs_len))
  362. return -ERANGE;
  363. cr = get_zeroed_page(GFP_KERNEL);
  364. if (!cr)
  365. return -ENOMEM;
  366. rc = cxl_h_get_config(afu->guest->handle, cr_idx, offset,
  367. virt_to_phys((void *)cr), sz);
  368. if (rc)
  369. goto err;
  370. switch (sz) {
  371. case 1:
  372. c = *((char *) cr);
  373. *val = c;
  374. break;
  375. case 2:
  376. *val = in_le16((u16 *)cr);
  377. break;
  378. case 4:
  379. *val = in_le32((unsigned *)cr);
  380. break;
  381. case 8:
  382. *val = in_le64((u64 *)cr);
  383. break;
  384. default:
  385. WARN_ON(1);
  386. }
  387. err:
  388. free_page(cr);
  389. return rc;
  390. }
  391. static int guest_afu_cr_read32(struct cxl_afu *afu, int cr_idx, u64 offset,
  392. u32 *out)
  393. {
  394. int rc;
  395. u64 val;
  396. rc = _guest_afu_cr_readXX(4, afu, cr_idx, offset, &val);
  397. if (!rc)
  398. *out = (u32) val;
  399. return rc;
  400. }
  401. static int guest_afu_cr_read16(struct cxl_afu *afu, int cr_idx, u64 offset,
  402. u16 *out)
  403. {
  404. int rc;
  405. u64 val;
  406. rc = _guest_afu_cr_readXX(2, afu, cr_idx, offset, &val);
  407. if (!rc)
  408. *out = (u16) val;
  409. return rc;
  410. }
  411. static int guest_afu_cr_read8(struct cxl_afu *afu, int cr_idx, u64 offset,
  412. u8 *out)
  413. {
  414. int rc;
  415. u64 val;
  416. rc = _guest_afu_cr_readXX(1, afu, cr_idx, offset, &val);
  417. if (!rc)
  418. *out = (u8) val;
  419. return rc;
  420. }
  421. static int guest_afu_cr_read64(struct cxl_afu *afu, int cr_idx, u64 offset,
  422. u64 *out)
  423. {
  424. return _guest_afu_cr_readXX(8, afu, cr_idx, offset, out);
  425. }
  426. static int guest_afu_cr_write32(struct cxl_afu *afu, int cr, u64 off, u32 in)
  427. {
  428. /* config record is not writable from guest */
  429. return -EPERM;
  430. }
  431. static int guest_afu_cr_write16(struct cxl_afu *afu, int cr, u64 off, u16 in)
  432. {
  433. /* config record is not writable from guest */
  434. return -EPERM;
  435. }
  436. static int guest_afu_cr_write8(struct cxl_afu *afu, int cr, u64 off, u8 in)
  437. {
  438. /* config record is not writable from guest */
  439. return -EPERM;
  440. }
  441. static int attach_afu_directed(struct cxl_context *ctx, u64 wed, u64 amr)
  442. {
  443. struct cxl_process_element_hcall *elem;
  444. struct cxl *adapter = ctx->afu->adapter;
  445. const struct cred *cred;
  446. u32 pid, idx;
  447. int rc, r, i;
  448. u64 mmio_addr, mmio_size;
  449. __be64 flags = 0;
  450. /* Must be 8 byte aligned and cannot cross a 4096 byte boundary */
  451. if (!(elem = (struct cxl_process_element_hcall *)
  452. get_zeroed_page(GFP_KERNEL)))
  453. return -ENOMEM;
  454. elem->version = cpu_to_be64(CXL_PROCESS_ELEMENT_VERSION);
  455. if (ctx->kernel) {
  456. pid = 0;
  457. flags |= CXL_PE_TRANSLATION_ENABLED;
  458. flags |= CXL_PE_PRIVILEGED_PROCESS;
  459. if (mfmsr() & MSR_SF)
  460. flags |= CXL_PE_64_BIT;
  461. } else {
  462. pid = current->pid;
  463. flags |= CXL_PE_PROBLEM_STATE;
  464. flags |= CXL_PE_TRANSLATION_ENABLED;
  465. if (!test_tsk_thread_flag(current, TIF_32BIT))
  466. flags |= CXL_PE_64_BIT;
  467. cred = get_current_cred();
  468. if (uid_eq(cred->euid, GLOBAL_ROOT_UID))
  469. flags |= CXL_PE_PRIVILEGED_PROCESS;
  470. put_cred(cred);
  471. }
  472. elem->flags = cpu_to_be64(flags);
  473. elem->common.tid = cpu_to_be32(0); /* Unused */
  474. elem->common.pid = cpu_to_be32(pid);
  475. elem->common.csrp = cpu_to_be64(0); /* disable */
  476. elem->common.u.psl8.aurp0 = cpu_to_be64(0); /* disable */
  477. elem->common.u.psl8.aurp1 = cpu_to_be64(0); /* disable */
  478. cxl_prefault(ctx, wed);
  479. elem->common.u.psl8.sstp0 = cpu_to_be64(ctx->sstp0);
  480. elem->common.u.psl8.sstp1 = cpu_to_be64(ctx->sstp1);
  481. /*
  482. * Ensure we have at least one interrupt allocated to take faults for
  483. * kernel contexts that may not have allocated any AFU IRQs at all:
  484. */
  485. if (ctx->irqs.range[0] == 0) {
  486. rc = afu_register_irqs(ctx, 0);
  487. if (rc)
  488. goto out_free;
  489. }
  490. for (r = 0; r < CXL_IRQ_RANGES; r++) {
  491. for (i = 0; i < ctx->irqs.range[r]; i++) {
  492. if (r == 0 && i == 0) {
  493. elem->pslVirtualIsn = cpu_to_be32(ctx->irqs.offset[0]);
  494. } else {
  495. idx = ctx->irqs.offset[r] + i - adapter->guest->irq_base_offset;
  496. elem->applicationVirtualIsnBitmap[idx / 8] |= 0x80 >> (idx % 8);
  497. }
  498. }
  499. }
  500. elem->common.amr = cpu_to_be64(amr);
  501. elem->common.wed = cpu_to_be64(wed);
  502. disable_afu_irqs(ctx);
  503. rc = cxl_h_attach_process(ctx->afu->guest->handle, elem,
  504. &ctx->process_token, &mmio_addr, &mmio_size);
  505. if (rc == H_SUCCESS) {
  506. if (ctx->master || !ctx->afu->pp_psa) {
  507. ctx->psn_phys = ctx->afu->psn_phys;
  508. ctx->psn_size = ctx->afu->adapter->ps_size;
  509. } else {
  510. ctx->psn_phys = mmio_addr;
  511. ctx->psn_size = mmio_size;
  512. }
  513. if (ctx->afu->pp_psa && mmio_size &&
  514. ctx->afu->pp_size == 0) {
  515. /*
  516. * There's no property in the device tree to read the
  517. * pp_size. We only find out at the 1st attach.
  518. * Compared to bare-metal, it is too late and we
  519. * should really lock here. However, on powerVM,
  520. * pp_size is really only used to display in /sys.
  521. * Being discussed with pHyp for their next release.
  522. */
  523. ctx->afu->pp_size = mmio_size;
  524. }
  525. /* from PAPR: process element is bytes 4-7 of process token */
  526. ctx->external_pe = ctx->process_token & 0xFFFFFFFF;
  527. pr_devel("CXL pe=%i is known as %i for pHyp, mmio_size=%#llx",
  528. ctx->pe, ctx->external_pe, ctx->psn_size);
  529. ctx->pe_inserted = true;
  530. enable_afu_irqs(ctx);
  531. }
  532. out_free:
  533. free_page((u64)elem);
  534. return rc;
  535. }
  536. static int guest_attach_process(struct cxl_context *ctx, bool kernel, u64 wed, u64 amr)
  537. {
  538. pr_devel("in %s\n", __func__);
  539. ctx->kernel = kernel;
  540. if (ctx->afu->current_mode == CXL_MODE_DIRECTED)
  541. return attach_afu_directed(ctx, wed, amr);
  542. /* dedicated mode not supported on FW840 */
  543. return -EINVAL;
  544. }
  545. static int detach_afu_directed(struct cxl_context *ctx)
  546. {
  547. if (!ctx->pe_inserted)
  548. return 0;
  549. if (cxl_h_detach_process(ctx->afu->guest->handle, ctx->process_token))
  550. return -1;
  551. return 0;
  552. }
  553. static int guest_detach_process(struct cxl_context *ctx)
  554. {
  555. pr_devel("in %s\n", __func__);
  556. trace_cxl_detach(ctx);
  557. if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
  558. return -EIO;
  559. if (ctx->afu->current_mode == CXL_MODE_DIRECTED)
  560. return detach_afu_directed(ctx);
  561. return -EINVAL;
  562. }
  563. static void guest_release_afu(struct device *dev)
  564. {
  565. struct cxl_afu *afu = to_cxl_afu(dev);
  566. pr_devel("%s\n", __func__);
  567. idr_destroy(&afu->contexts_idr);
  568. kfree(afu->guest);
  569. kfree(afu);
  570. }
  571. ssize_t cxl_guest_read_afu_vpd(struct cxl_afu *afu, void *buf, size_t len)
  572. {
  573. return guest_collect_vpd(NULL, afu, buf, len);
  574. }
  575. #define ERR_BUFF_MAX_COPY_SIZE PAGE_SIZE
  576. static ssize_t guest_afu_read_err_buffer(struct cxl_afu *afu, char *buf,
  577. loff_t off, size_t count)
  578. {
  579. void *tbuf = NULL;
  580. int rc = 0;
  581. tbuf = (void *) get_zeroed_page(GFP_KERNEL);
  582. if (!tbuf)
  583. return -ENOMEM;
  584. rc = cxl_h_get_afu_err(afu->guest->handle,
  585. off & 0x7,
  586. virt_to_phys(tbuf),
  587. count);
  588. if (rc)
  589. goto err;
  590. if (count > ERR_BUFF_MAX_COPY_SIZE)
  591. count = ERR_BUFF_MAX_COPY_SIZE - (off & 0x7);
  592. memcpy(buf, tbuf, count);
  593. err:
  594. free_page((u64)tbuf);
  595. return rc;
  596. }
  597. static int guest_afu_check_and_enable(struct cxl_afu *afu)
  598. {
  599. return 0;
  600. }
  601. static bool guest_support_attributes(const char *attr_name,
  602. enum cxl_attrs type)
  603. {
  604. switch (type) {
  605. case CXL_ADAPTER_ATTRS:
  606. if ((strcmp(attr_name, "base_image") == 0) ||
  607. (strcmp(attr_name, "load_image_on_perst") == 0) ||
  608. (strcmp(attr_name, "perst_reloads_same_image") == 0) ||
  609. (strcmp(attr_name, "image_loaded") == 0))
  610. return false;
  611. break;
  612. case CXL_AFU_MASTER_ATTRS:
  613. if ((strcmp(attr_name, "pp_mmio_off") == 0))
  614. return false;
  615. break;
  616. case CXL_AFU_ATTRS:
  617. break;
  618. default:
  619. break;
  620. }
  621. return true;
  622. }
  623. static int activate_afu_directed(struct cxl_afu *afu)
  624. {
  625. int rc;
  626. dev_info(&afu->dev, "Activating AFU(%d) directed mode\n", afu->slice);
  627. afu->current_mode = CXL_MODE_DIRECTED;
  628. afu->num_procs = afu->max_procs_virtualised;
  629. if ((rc = cxl_chardev_m_afu_add(afu)))
  630. return rc;
  631. if ((rc = cxl_sysfs_afu_m_add(afu)))
  632. goto err;
  633. if ((rc = cxl_chardev_s_afu_add(afu)))
  634. goto err1;
  635. return 0;
  636. err1:
  637. cxl_sysfs_afu_m_remove(afu);
  638. err:
  639. cxl_chardev_afu_remove(afu);
  640. return rc;
  641. }
  642. static int guest_afu_activate_mode(struct cxl_afu *afu, int mode)
  643. {
  644. if (!mode)
  645. return 0;
  646. if (!(mode & afu->modes_supported))
  647. return -EINVAL;
  648. if (mode == CXL_MODE_DIRECTED)
  649. return activate_afu_directed(afu);
  650. if (mode == CXL_MODE_DEDICATED)
  651. dev_err(&afu->dev, "Dedicated mode not supported\n");
  652. return -EINVAL;
  653. }
  654. static int deactivate_afu_directed(struct cxl_afu *afu)
  655. {
  656. dev_info(&afu->dev, "Deactivating AFU(%d) directed mode\n", afu->slice);
  657. afu->current_mode = 0;
  658. afu->num_procs = 0;
  659. cxl_sysfs_afu_m_remove(afu);
  660. cxl_chardev_afu_remove(afu);
  661. cxl_ops->afu_reset(afu);
  662. return 0;
  663. }
  664. static int guest_afu_deactivate_mode(struct cxl_afu *afu, int mode)
  665. {
  666. if (!mode)
  667. return 0;
  668. if (!(mode & afu->modes_supported))
  669. return -EINVAL;
  670. if (mode == CXL_MODE_DIRECTED)
  671. return deactivate_afu_directed(afu);
  672. return 0;
  673. }
  674. static int guest_afu_reset(struct cxl_afu *afu)
  675. {
  676. pr_devel("AFU(%d) reset request\n", afu->slice);
  677. return cxl_h_reset_afu(afu->guest->handle);
  678. }
  679. static int guest_map_slice_regs(struct cxl_afu *afu)
  680. {
  681. if (!(afu->p2n_mmio = ioremap(afu->guest->p2n_phys, afu->guest->p2n_size))) {
  682. dev_err(&afu->dev, "Error mapping AFU(%d) MMIO regions\n",
  683. afu->slice);
  684. return -ENOMEM;
  685. }
  686. return 0;
  687. }
  688. static void guest_unmap_slice_regs(struct cxl_afu *afu)
  689. {
  690. if (afu->p2n_mmio)
  691. iounmap(afu->p2n_mmio);
  692. }
  693. static int afu_update_state(struct cxl_afu *afu)
  694. {
  695. int rc, cur_state;
  696. rc = afu_read_error_state(afu, &cur_state);
  697. if (rc)
  698. return rc;
  699. if (afu->guest->previous_state == cur_state)
  700. return 0;
  701. pr_devel("AFU(%d) update state to %#x\n", afu->slice, cur_state);
  702. switch (cur_state) {
  703. case H_STATE_NORMAL:
  704. afu->guest->previous_state = cur_state;
  705. break;
  706. case H_STATE_DISABLE:
  707. pci_error_handlers(afu, CXL_ERROR_DETECTED_EVENT,
  708. pci_channel_io_frozen);
  709. cxl_context_detach_all(afu);
  710. if ((rc = cxl_ops->afu_reset(afu)))
  711. pr_devel("reset hcall failed %d\n", rc);
  712. rc = afu_read_error_state(afu, &cur_state);
  713. if (!rc && cur_state == H_STATE_NORMAL) {
  714. pci_error_handlers(afu, CXL_SLOT_RESET_EVENT,
  715. pci_channel_io_normal);
  716. pci_error_handlers(afu, CXL_RESUME_EVENT, 0);
  717. }
  718. afu->guest->previous_state = 0;
  719. break;
  720. case H_STATE_TEMP_UNAVAILABLE:
  721. afu->guest->previous_state = cur_state;
  722. break;
  723. case H_STATE_PERM_UNAVAILABLE:
  724. dev_err(&afu->dev, "AFU is in permanent error state\n");
  725. pci_error_handlers(afu, CXL_ERROR_DETECTED_EVENT,
  726. pci_channel_io_perm_failure);
  727. afu->guest->previous_state = cur_state;
  728. break;
  729. default:
  730. pr_err("Unexpected AFU(%d) error state: %#x\n",
  731. afu->slice, cur_state);
  732. return -EINVAL;
  733. }
  734. return rc;
  735. }
  736. static void afu_handle_errstate(struct work_struct *work)
  737. {
  738. struct cxl_afu_guest *afu_guest =
  739. container_of(to_delayed_work(work), struct cxl_afu_guest, work_err);
  740. if (!afu_update_state(afu_guest->parent) &&
  741. afu_guest->previous_state == H_STATE_PERM_UNAVAILABLE)
  742. return;
  743. if (afu_guest->handle_err)
  744. schedule_delayed_work(&afu_guest->work_err,
  745. msecs_to_jiffies(3000));
  746. }
  747. static bool guest_link_ok(struct cxl *cxl, struct cxl_afu *afu)
  748. {
  749. int state;
  750. if (afu && (!afu_read_error_state(afu, &state))) {
  751. if (state == H_STATE_NORMAL)
  752. return true;
  753. }
  754. return false;
  755. }
  756. static int afu_properties_look_ok(struct cxl_afu *afu)
  757. {
  758. if (afu->pp_irqs < 0) {
  759. dev_err(&afu->dev, "Unexpected per-process minimum interrupt value\n");
  760. return -EINVAL;
  761. }
  762. if (afu->max_procs_virtualised < 1) {
  763. dev_err(&afu->dev, "Unexpected max number of processes virtualised value\n");
  764. return -EINVAL;
  765. }
  766. return 0;
  767. }
  768. int cxl_guest_init_afu(struct cxl *adapter, int slice, struct device_node *afu_np)
  769. {
  770. struct cxl_afu *afu;
  771. bool free = true;
  772. int rc;
  773. pr_devel("in %s - AFU(%d)\n", __func__, slice);
  774. if (!(afu = cxl_alloc_afu(adapter, slice)))
  775. return -ENOMEM;
  776. if (!(afu->guest = kzalloc(sizeof(struct cxl_afu_guest), GFP_KERNEL))) {
  777. kfree(afu);
  778. return -ENOMEM;
  779. }
  780. if ((rc = dev_set_name(&afu->dev, "afu%i.%i",
  781. adapter->adapter_num,
  782. slice)))
  783. goto err1;
  784. adapter->slices++;
  785. if ((rc = cxl_of_read_afu_handle(afu, afu_np)))
  786. goto err1;
  787. if ((rc = cxl_ops->afu_reset(afu)))
  788. goto err1;
  789. if ((rc = cxl_of_read_afu_properties(afu, afu_np)))
  790. goto err1;
  791. if ((rc = afu_properties_look_ok(afu)))
  792. goto err1;
  793. if ((rc = guest_map_slice_regs(afu)))
  794. goto err1;
  795. if ((rc = guest_register_serr_irq(afu)))
  796. goto err2;
  797. /*
  798. * After we call this function we must not free the afu directly, even
  799. * if it returns an error!
  800. */
  801. if ((rc = cxl_register_afu(afu)))
  802. goto err_put1;
  803. if ((rc = cxl_sysfs_afu_add(afu)))
  804. goto err_put1;
  805. /*
  806. * pHyp doesn't expose the programming models supported by the
  807. * AFU. pHyp currently only supports directed mode. If it adds
  808. * dedicated mode later, this version of cxl has no way to
  809. * detect it. So we'll initialize the driver, but the first
  810. * attach will fail.
  811. * Being discussed with pHyp to do better (likely new property)
  812. */
  813. if (afu->max_procs_virtualised == 1)
  814. afu->modes_supported = CXL_MODE_DEDICATED;
  815. else
  816. afu->modes_supported = CXL_MODE_DIRECTED;
  817. if ((rc = cxl_afu_select_best_mode(afu)))
  818. goto err_put2;
  819. adapter->afu[afu->slice] = afu;
  820. afu->enabled = true;
  821. /*
  822. * wake up the cpu periodically to check the state
  823. * of the AFU using "afu" stored in the guest structure.
  824. */
  825. afu->guest->parent = afu;
  826. afu->guest->handle_err = true;
  827. INIT_DELAYED_WORK(&afu->guest->work_err, afu_handle_errstate);
  828. schedule_delayed_work(&afu->guest->work_err, msecs_to_jiffies(1000));
  829. if ((rc = cxl_pci_vphb_add(afu)))
  830. dev_info(&afu->dev, "Can't register vPHB\n");
  831. return 0;
  832. err_put2:
  833. cxl_sysfs_afu_remove(afu);
  834. err_put1:
  835. device_unregister(&afu->dev);
  836. free = false;
  837. guest_release_serr_irq(afu);
  838. err2:
  839. guest_unmap_slice_regs(afu);
  840. err1:
  841. if (free) {
  842. kfree(afu->guest);
  843. kfree(afu);
  844. }
  845. return rc;
  846. }
  847. void cxl_guest_remove_afu(struct cxl_afu *afu)
  848. {
  849. if (!afu)
  850. return;
  851. /* flush and stop pending job */
  852. afu->guest->handle_err = false;
  853. flush_delayed_work(&afu->guest->work_err);
  854. cxl_pci_vphb_remove(afu);
  855. cxl_sysfs_afu_remove(afu);
  856. spin_lock(&afu->adapter->afu_list_lock);
  857. afu->adapter->afu[afu->slice] = NULL;
  858. spin_unlock(&afu->adapter->afu_list_lock);
  859. cxl_context_detach_all(afu);
  860. cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
  861. guest_release_serr_irq(afu);
  862. guest_unmap_slice_regs(afu);
  863. device_unregister(&afu->dev);
  864. }
  865. static void free_adapter(struct cxl *adapter)
  866. {
  867. struct irq_avail *cur;
  868. int i;
  869. if (adapter->guest) {
  870. if (adapter->guest->irq_avail) {
  871. for (i = 0; i < adapter->guest->irq_nranges; i++) {
  872. cur = &adapter->guest->irq_avail[i];
  873. kfree(cur->bitmap);
  874. }
  875. kfree(adapter->guest->irq_avail);
  876. }
  877. kfree(adapter->guest->status);
  878. kfree(adapter->guest);
  879. }
  880. cxl_remove_adapter_nr(adapter);
  881. kfree(adapter);
  882. }
  883. static int properties_look_ok(struct cxl *adapter)
  884. {
  885. /* The absence of this property means that the operational
  886. * status is unknown or okay
  887. */
  888. if (strlen(adapter->guest->status) &&
  889. strcmp(adapter->guest->status, "okay")) {
  890. pr_err("ABORTING:Bad operational status of the device\n");
  891. return -EINVAL;
  892. }
  893. return 0;
  894. }
  895. ssize_t cxl_guest_read_adapter_vpd(struct cxl *adapter, void *buf, size_t len)
  896. {
  897. return guest_collect_vpd(adapter, NULL, buf, len);
  898. }
  899. void cxl_guest_remove_adapter(struct cxl *adapter)
  900. {
  901. pr_devel("in %s\n", __func__);
  902. cxl_sysfs_adapter_remove(adapter);
  903. cxl_guest_remove_chardev(adapter);
  904. device_unregister(&adapter->dev);
  905. }
  906. static void release_adapter(struct device *dev)
  907. {
  908. free_adapter(to_cxl_adapter(dev));
  909. }
  910. struct cxl *cxl_guest_init_adapter(struct device_node *np, struct platform_device *pdev)
  911. {
  912. struct cxl *adapter;
  913. bool free = true;
  914. int rc;
  915. if (!(adapter = cxl_alloc_adapter()))
  916. return ERR_PTR(-ENOMEM);
  917. if (!(adapter->guest = kzalloc(sizeof(struct cxl_guest), GFP_KERNEL))) {
  918. free_adapter(adapter);
  919. return ERR_PTR(-ENOMEM);
  920. }
  921. adapter->slices = 0;
  922. adapter->guest->pdev = pdev;
  923. adapter->dev.parent = &pdev->dev;
  924. adapter->dev.release = release_adapter;
  925. dev_set_drvdata(&pdev->dev, adapter);
  926. /*
  927. * Hypervisor controls PSL timebase initialization (p1 register).
  928. * On FW840, PSL is initialized.
  929. */
  930. adapter->psl_timebase_synced = true;
  931. if ((rc = cxl_of_read_adapter_handle(adapter, np)))
  932. goto err1;
  933. if ((rc = cxl_of_read_adapter_properties(adapter, np)))
  934. goto err1;
  935. if ((rc = properties_look_ok(adapter)))
  936. goto err1;
  937. if ((rc = cxl_guest_add_chardev(adapter)))
  938. goto err1;
  939. /*
  940. * After we call this function we must not free the adapter directly,
  941. * even if it returns an error!
  942. */
  943. if ((rc = cxl_register_adapter(adapter)))
  944. goto err_put1;
  945. if ((rc = cxl_sysfs_adapter_add(adapter)))
  946. goto err_put1;
  947. /* release the context lock as the adapter is configured */
  948. cxl_adapter_context_unlock(adapter);
  949. return adapter;
  950. err_put1:
  951. device_unregister(&adapter->dev);
  952. free = false;
  953. cxl_guest_remove_chardev(adapter);
  954. err1:
  955. if (free)
  956. free_adapter(adapter);
  957. return ERR_PTR(rc);
  958. }
  959. void cxl_guest_reload_module(struct cxl *adapter)
  960. {
  961. struct platform_device *pdev;
  962. pdev = adapter->guest->pdev;
  963. cxl_guest_remove_adapter(adapter);
  964. cxl_of_probe(pdev);
  965. }
  966. const struct cxl_backend_ops cxl_guest_ops = {
  967. .module = THIS_MODULE,
  968. .adapter_reset = guest_reset,
  969. .alloc_one_irq = guest_alloc_one_irq,
  970. .release_one_irq = guest_release_one_irq,
  971. .alloc_irq_ranges = guest_alloc_irq_ranges,
  972. .release_irq_ranges = guest_release_irq_ranges,
  973. .setup_irq = NULL,
  974. .handle_psl_slice_error = guest_handle_psl_slice_error,
  975. .psl_interrupt = guest_psl_irq,
  976. .ack_irq = guest_ack_irq,
  977. .attach_process = guest_attach_process,
  978. .detach_process = guest_detach_process,
  979. .update_ivtes = NULL,
  980. .support_attributes = guest_support_attributes,
  981. .link_ok = guest_link_ok,
  982. .release_afu = guest_release_afu,
  983. .afu_read_err_buffer = guest_afu_read_err_buffer,
  984. .afu_check_and_enable = guest_afu_check_and_enable,
  985. .afu_activate_mode = guest_afu_activate_mode,
  986. .afu_deactivate_mode = guest_afu_deactivate_mode,
  987. .afu_reset = guest_afu_reset,
  988. .afu_cr_read8 = guest_afu_cr_read8,
  989. .afu_cr_read16 = guest_afu_cr_read16,
  990. .afu_cr_read32 = guest_afu_cr_read32,
  991. .afu_cr_read64 = guest_afu_cr_read64,
  992. .afu_cr_write8 = guest_afu_cr_write8,
  993. .afu_cr_write16 = guest_afu_cr_write16,
  994. .afu_cr_write32 = guest_afu_cr_write32,
  995. .read_adapter_vpd = cxl_guest_read_adapter_vpd,
  996. };