guest.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  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. if (afu->crs_len < 0) {
  767. dev_err(&afu->dev, "Unexpected configuration record size value\n");
  768. return -EINVAL;
  769. }
  770. return 0;
  771. }
  772. int cxl_guest_init_afu(struct cxl *adapter, int slice, struct device_node *afu_np)
  773. {
  774. struct cxl_afu *afu;
  775. bool free = true;
  776. int rc;
  777. pr_devel("in %s - AFU(%d)\n", __func__, slice);
  778. if (!(afu = cxl_alloc_afu(adapter, slice)))
  779. return -ENOMEM;
  780. if (!(afu->guest = kzalloc(sizeof(struct cxl_afu_guest), GFP_KERNEL))) {
  781. kfree(afu);
  782. return -ENOMEM;
  783. }
  784. if ((rc = dev_set_name(&afu->dev, "afu%i.%i",
  785. adapter->adapter_num,
  786. slice)))
  787. goto err1;
  788. adapter->slices++;
  789. if ((rc = cxl_of_read_afu_handle(afu, afu_np)))
  790. goto err1;
  791. if ((rc = cxl_ops->afu_reset(afu)))
  792. goto err1;
  793. if ((rc = cxl_of_read_afu_properties(afu, afu_np)))
  794. goto err1;
  795. if ((rc = afu_properties_look_ok(afu)))
  796. goto err1;
  797. if ((rc = guest_map_slice_regs(afu)))
  798. goto err1;
  799. if ((rc = guest_register_serr_irq(afu)))
  800. goto err2;
  801. /*
  802. * After we call this function we must not free the afu directly, even
  803. * if it returns an error!
  804. */
  805. if ((rc = cxl_register_afu(afu)))
  806. goto err_put1;
  807. if ((rc = cxl_sysfs_afu_add(afu)))
  808. goto err_put1;
  809. /*
  810. * pHyp doesn't expose the programming models supported by the
  811. * AFU. pHyp currently only supports directed mode. If it adds
  812. * dedicated mode later, this version of cxl has no way to
  813. * detect it. So we'll initialize the driver, but the first
  814. * attach will fail.
  815. * Being discussed with pHyp to do better (likely new property)
  816. */
  817. if (afu->max_procs_virtualised == 1)
  818. afu->modes_supported = CXL_MODE_DEDICATED;
  819. else
  820. afu->modes_supported = CXL_MODE_DIRECTED;
  821. if ((rc = cxl_afu_select_best_mode(afu)))
  822. goto err_put2;
  823. adapter->afu[afu->slice] = afu;
  824. afu->enabled = true;
  825. /*
  826. * wake up the cpu periodically to check the state
  827. * of the AFU using "afu" stored in the guest structure.
  828. */
  829. afu->guest->parent = afu;
  830. afu->guest->handle_err = true;
  831. INIT_DELAYED_WORK(&afu->guest->work_err, afu_handle_errstate);
  832. schedule_delayed_work(&afu->guest->work_err, msecs_to_jiffies(1000));
  833. if ((rc = cxl_pci_vphb_add(afu)))
  834. dev_info(&afu->dev, "Can't register vPHB\n");
  835. return 0;
  836. err_put2:
  837. cxl_sysfs_afu_remove(afu);
  838. err_put1:
  839. device_unregister(&afu->dev);
  840. free = false;
  841. guest_release_serr_irq(afu);
  842. err2:
  843. guest_unmap_slice_regs(afu);
  844. err1:
  845. if (free) {
  846. kfree(afu->guest);
  847. kfree(afu);
  848. }
  849. return rc;
  850. }
  851. void cxl_guest_remove_afu(struct cxl_afu *afu)
  852. {
  853. pr_devel("in %s - AFU(%d)\n", __func__, afu->slice);
  854. if (!afu)
  855. return;
  856. /* flush and stop pending job */
  857. afu->guest->handle_err = false;
  858. flush_delayed_work(&afu->guest->work_err);
  859. cxl_pci_vphb_remove(afu);
  860. cxl_sysfs_afu_remove(afu);
  861. spin_lock(&afu->adapter->afu_list_lock);
  862. afu->adapter->afu[afu->slice] = NULL;
  863. spin_unlock(&afu->adapter->afu_list_lock);
  864. cxl_context_detach_all(afu);
  865. cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
  866. guest_release_serr_irq(afu);
  867. guest_unmap_slice_regs(afu);
  868. device_unregister(&afu->dev);
  869. }
  870. static void free_adapter(struct cxl *adapter)
  871. {
  872. struct irq_avail *cur;
  873. int i;
  874. if (adapter->guest) {
  875. if (adapter->guest->irq_avail) {
  876. for (i = 0; i < adapter->guest->irq_nranges; i++) {
  877. cur = &adapter->guest->irq_avail[i];
  878. kfree(cur->bitmap);
  879. }
  880. kfree(adapter->guest->irq_avail);
  881. }
  882. kfree(adapter->guest->status);
  883. kfree(adapter->guest);
  884. }
  885. cxl_remove_adapter_nr(adapter);
  886. kfree(adapter);
  887. }
  888. static int properties_look_ok(struct cxl *adapter)
  889. {
  890. /* The absence of this property means that the operational
  891. * status is unknown or okay
  892. */
  893. if (strlen(adapter->guest->status) &&
  894. strcmp(adapter->guest->status, "okay")) {
  895. pr_err("ABORTING:Bad operational status of the device\n");
  896. return -EINVAL;
  897. }
  898. return 0;
  899. }
  900. ssize_t cxl_guest_read_adapter_vpd(struct cxl *adapter, void *buf, size_t len)
  901. {
  902. return guest_collect_vpd(adapter, NULL, buf, len);
  903. }
  904. void cxl_guest_remove_adapter(struct cxl *adapter)
  905. {
  906. pr_devel("in %s\n", __func__);
  907. cxl_sysfs_adapter_remove(adapter);
  908. cxl_guest_remove_chardev(adapter);
  909. device_unregister(&adapter->dev);
  910. }
  911. static void release_adapter(struct device *dev)
  912. {
  913. free_adapter(to_cxl_adapter(dev));
  914. }
  915. struct cxl *cxl_guest_init_adapter(struct device_node *np, struct platform_device *pdev)
  916. {
  917. struct cxl *adapter;
  918. bool free = true;
  919. int rc;
  920. if (!(adapter = cxl_alloc_adapter()))
  921. return ERR_PTR(-ENOMEM);
  922. if (!(adapter->guest = kzalloc(sizeof(struct cxl_guest), GFP_KERNEL))) {
  923. free_adapter(adapter);
  924. return ERR_PTR(-ENOMEM);
  925. }
  926. adapter->slices = 0;
  927. adapter->guest->pdev = pdev;
  928. adapter->dev.parent = &pdev->dev;
  929. adapter->dev.release = release_adapter;
  930. dev_set_drvdata(&pdev->dev, adapter);
  931. /*
  932. * Hypervisor controls PSL timebase initialization (p1 register).
  933. * On FW840, PSL is initialized.
  934. */
  935. adapter->psl_timebase_synced = true;
  936. if ((rc = cxl_of_read_adapter_handle(adapter, np)))
  937. goto err1;
  938. if ((rc = cxl_of_read_adapter_properties(adapter, np)))
  939. goto err1;
  940. if ((rc = properties_look_ok(adapter)))
  941. goto err1;
  942. if ((rc = cxl_guest_add_chardev(adapter)))
  943. goto err1;
  944. /*
  945. * After we call this function we must not free the adapter directly,
  946. * even if it returns an error!
  947. */
  948. if ((rc = cxl_register_adapter(adapter)))
  949. goto err_put1;
  950. if ((rc = cxl_sysfs_adapter_add(adapter)))
  951. goto err_put1;
  952. /* release the context lock as the adapter is configured */
  953. cxl_adapter_context_unlock(adapter);
  954. return adapter;
  955. err_put1:
  956. device_unregister(&adapter->dev);
  957. free = false;
  958. cxl_guest_remove_chardev(adapter);
  959. err1:
  960. if (free)
  961. free_adapter(adapter);
  962. return ERR_PTR(rc);
  963. }
  964. void cxl_guest_reload_module(struct cxl *adapter)
  965. {
  966. struct platform_device *pdev;
  967. pdev = adapter->guest->pdev;
  968. cxl_guest_remove_adapter(adapter);
  969. cxl_of_probe(pdev);
  970. }
  971. const struct cxl_backend_ops cxl_guest_ops = {
  972. .module = THIS_MODULE,
  973. .adapter_reset = guest_reset,
  974. .alloc_one_irq = guest_alloc_one_irq,
  975. .release_one_irq = guest_release_one_irq,
  976. .alloc_irq_ranges = guest_alloc_irq_ranges,
  977. .release_irq_ranges = guest_release_irq_ranges,
  978. .setup_irq = NULL,
  979. .handle_psl_slice_error = guest_handle_psl_slice_error,
  980. .psl_interrupt = guest_psl_irq,
  981. .ack_irq = guest_ack_irq,
  982. .attach_process = guest_attach_process,
  983. .detach_process = guest_detach_process,
  984. .update_ivtes = NULL,
  985. .support_attributes = guest_support_attributes,
  986. .link_ok = guest_link_ok,
  987. .release_afu = guest_release_afu,
  988. .afu_read_err_buffer = guest_afu_read_err_buffer,
  989. .afu_check_and_enable = guest_afu_check_and_enable,
  990. .afu_activate_mode = guest_afu_activate_mode,
  991. .afu_deactivate_mode = guest_afu_deactivate_mode,
  992. .afu_reset = guest_afu_reset,
  993. .afu_cr_read8 = guest_afu_cr_read8,
  994. .afu_cr_read16 = guest_afu_cr_read16,
  995. .afu_cr_read32 = guest_afu_cr_read32,
  996. .afu_cr_read64 = guest_afu_cr_read64,
  997. .afu_cr_write8 = guest_afu_cr_write8,
  998. .afu_cr_write16 = guest_afu_cr_write16,
  999. .afu_cr_write32 = guest_afu_cr_write32,
  1000. .read_adapter_vpd = cxl_guest_read_adapter_vpd,
  1001. };