guest.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  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. spin_lock(&adapter->afu_list_lock);
  231. for (i = 0; i < adapter->slices; i++) {
  232. if ((afu = adapter->afu[i])) {
  233. pci_error_handlers(afu, CXL_ERROR_DETECTED_EVENT,
  234. pci_channel_io_frozen);
  235. cxl_context_detach_all(afu);
  236. }
  237. }
  238. rc = cxl_h_reset_adapter(adapter->guest->handle);
  239. for (i = 0; i < adapter->slices; i++) {
  240. if (!rc && (afu = adapter->afu[i])) {
  241. pci_error_handlers(afu, CXL_SLOT_RESET_EVENT,
  242. pci_channel_io_normal);
  243. pci_error_handlers(afu, CXL_RESUME_EVENT, 0);
  244. }
  245. }
  246. spin_unlock(&adapter->afu_list_lock);
  247. return rc;
  248. }
  249. static int guest_alloc_one_irq(struct cxl *adapter)
  250. {
  251. int irq;
  252. spin_lock(&adapter->guest->irq_alloc_lock);
  253. if (irq_alloc_range(adapter, 1, &irq))
  254. irq = -ENOSPC;
  255. spin_unlock(&adapter->guest->irq_alloc_lock);
  256. return irq;
  257. }
  258. static void guest_release_one_irq(struct cxl *adapter, int irq)
  259. {
  260. spin_lock(&adapter->guest->irq_alloc_lock);
  261. irq_free_range(adapter, irq, 1);
  262. spin_unlock(&adapter->guest->irq_alloc_lock);
  263. }
  264. static int guest_alloc_irq_ranges(struct cxl_irq_ranges *irqs,
  265. struct cxl *adapter, unsigned int num)
  266. {
  267. int i, try, irq;
  268. memset(irqs, 0, sizeof(struct cxl_irq_ranges));
  269. spin_lock(&adapter->guest->irq_alloc_lock);
  270. for (i = 0; i < CXL_IRQ_RANGES && num; i++) {
  271. try = num;
  272. while (try) {
  273. if (irq_alloc_range(adapter, try, &irq) == 0)
  274. break;
  275. try /= 2;
  276. }
  277. if (!try)
  278. goto error;
  279. irqs->offset[i] = irq;
  280. irqs->range[i] = try;
  281. num -= try;
  282. }
  283. if (num)
  284. goto error;
  285. spin_unlock(&adapter->guest->irq_alloc_lock);
  286. return 0;
  287. error:
  288. for (i = 0; i < CXL_IRQ_RANGES; i++)
  289. irq_free_range(adapter, irqs->offset[i], irqs->range[i]);
  290. spin_unlock(&adapter->guest->irq_alloc_lock);
  291. return -ENOSPC;
  292. }
  293. static void guest_release_irq_ranges(struct cxl_irq_ranges *irqs,
  294. struct cxl *adapter)
  295. {
  296. int i;
  297. spin_lock(&adapter->guest->irq_alloc_lock);
  298. for (i = 0; i < CXL_IRQ_RANGES; i++)
  299. irq_free_range(adapter, irqs->offset[i], irqs->range[i]);
  300. spin_unlock(&adapter->guest->irq_alloc_lock);
  301. }
  302. static int guest_register_serr_irq(struct cxl_afu *afu)
  303. {
  304. afu->err_irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
  305. dev_name(&afu->dev));
  306. if (!afu->err_irq_name)
  307. return -ENOMEM;
  308. if (!(afu->serr_virq = cxl_map_irq(afu->adapter, afu->serr_hwirq,
  309. guest_slice_irq_err, afu, afu->err_irq_name))) {
  310. kfree(afu->err_irq_name);
  311. afu->err_irq_name = NULL;
  312. return -ENOMEM;
  313. }
  314. return 0;
  315. }
  316. static void guest_release_serr_irq(struct cxl_afu *afu)
  317. {
  318. cxl_unmap_irq(afu->serr_virq, afu);
  319. cxl_ops->release_one_irq(afu->adapter, afu->serr_hwirq);
  320. kfree(afu->err_irq_name);
  321. }
  322. static int guest_ack_irq(struct cxl_context *ctx, u64 tfc, u64 psl_reset_mask)
  323. {
  324. return cxl_h_control_faults(ctx->afu->guest->handle, ctx->process_token,
  325. tfc >> 32, (psl_reset_mask != 0));
  326. }
  327. static void disable_afu_irqs(struct cxl_context *ctx)
  328. {
  329. irq_hw_number_t hwirq;
  330. unsigned int virq;
  331. int r, i;
  332. pr_devel("Disabling AFU(%d) interrupts\n", ctx->afu->slice);
  333. for (r = 0; r < CXL_IRQ_RANGES; r++) {
  334. hwirq = ctx->irqs.offset[r];
  335. for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
  336. virq = irq_find_mapping(NULL, hwirq);
  337. disable_irq(virq);
  338. }
  339. }
  340. }
  341. static void enable_afu_irqs(struct cxl_context *ctx)
  342. {
  343. irq_hw_number_t hwirq;
  344. unsigned int virq;
  345. int r, i;
  346. pr_devel("Enabling AFU(%d) interrupts\n", ctx->afu->slice);
  347. for (r = 0; r < CXL_IRQ_RANGES; r++) {
  348. hwirq = ctx->irqs.offset[r];
  349. for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
  350. virq = irq_find_mapping(NULL, hwirq);
  351. enable_irq(virq);
  352. }
  353. }
  354. }
  355. static int _guest_afu_cr_readXX(int sz, struct cxl_afu *afu, int cr_idx,
  356. u64 offset, u64 *val)
  357. {
  358. unsigned long cr;
  359. char c;
  360. int rc = 0;
  361. if (afu->crs_len < sz)
  362. return -ENOENT;
  363. if (unlikely(offset >= afu->crs_len))
  364. return -ERANGE;
  365. cr = get_zeroed_page(GFP_KERNEL);
  366. if (!cr)
  367. return -ENOMEM;
  368. rc = cxl_h_get_config(afu->guest->handle, cr_idx, offset,
  369. virt_to_phys((void *)cr), sz);
  370. if (rc)
  371. goto err;
  372. switch (sz) {
  373. case 1:
  374. c = *((char *) cr);
  375. *val = c;
  376. break;
  377. case 2:
  378. *val = in_le16((u16 *)cr);
  379. break;
  380. case 4:
  381. *val = in_le32((unsigned *)cr);
  382. break;
  383. case 8:
  384. *val = in_le64((u64 *)cr);
  385. break;
  386. default:
  387. WARN_ON(1);
  388. }
  389. err:
  390. free_page(cr);
  391. return rc;
  392. }
  393. static int guest_afu_cr_read32(struct cxl_afu *afu, int cr_idx, u64 offset,
  394. u32 *out)
  395. {
  396. int rc;
  397. u64 val;
  398. rc = _guest_afu_cr_readXX(4, afu, cr_idx, offset, &val);
  399. if (!rc)
  400. *out = (u32) val;
  401. return rc;
  402. }
  403. static int guest_afu_cr_read16(struct cxl_afu *afu, int cr_idx, u64 offset,
  404. u16 *out)
  405. {
  406. int rc;
  407. u64 val;
  408. rc = _guest_afu_cr_readXX(2, afu, cr_idx, offset, &val);
  409. if (!rc)
  410. *out = (u16) val;
  411. return rc;
  412. }
  413. static int guest_afu_cr_read8(struct cxl_afu *afu, int cr_idx, u64 offset,
  414. u8 *out)
  415. {
  416. int rc;
  417. u64 val;
  418. rc = _guest_afu_cr_readXX(1, afu, cr_idx, offset, &val);
  419. if (!rc)
  420. *out = (u8) val;
  421. return rc;
  422. }
  423. static int guest_afu_cr_read64(struct cxl_afu *afu, int cr_idx, u64 offset,
  424. u64 *out)
  425. {
  426. return _guest_afu_cr_readXX(8, afu, cr_idx, offset, out);
  427. }
  428. static int guest_afu_cr_write32(struct cxl_afu *afu, int cr, u64 off, u32 in)
  429. {
  430. /* config record is not writable from guest */
  431. return -EPERM;
  432. }
  433. static int guest_afu_cr_write16(struct cxl_afu *afu, int cr, u64 off, u16 in)
  434. {
  435. /* config record is not writable from guest */
  436. return -EPERM;
  437. }
  438. static int guest_afu_cr_write8(struct cxl_afu *afu, int cr, u64 off, u8 in)
  439. {
  440. /* config record is not writable from guest */
  441. return -EPERM;
  442. }
  443. static int attach_afu_directed(struct cxl_context *ctx, u64 wed, u64 amr)
  444. {
  445. struct cxl_process_element_hcall *elem;
  446. struct cxl *adapter = ctx->afu->adapter;
  447. const struct cred *cred;
  448. u32 pid, idx;
  449. int rc, r, i;
  450. u64 mmio_addr, mmio_size;
  451. __be64 flags = 0;
  452. /* Must be 8 byte aligned and cannot cross a 4096 byte boundary */
  453. if (!(elem = (struct cxl_process_element_hcall *)
  454. get_zeroed_page(GFP_KERNEL)))
  455. return -ENOMEM;
  456. elem->version = cpu_to_be64(CXL_PROCESS_ELEMENT_VERSION);
  457. if (ctx->kernel) {
  458. pid = 0;
  459. flags |= CXL_PE_TRANSLATION_ENABLED;
  460. flags |= CXL_PE_PRIVILEGED_PROCESS;
  461. if (mfmsr() & MSR_SF)
  462. flags |= CXL_PE_64_BIT;
  463. } else {
  464. pid = current->pid;
  465. flags |= CXL_PE_PROBLEM_STATE;
  466. flags |= CXL_PE_TRANSLATION_ENABLED;
  467. if (!test_tsk_thread_flag(current, TIF_32BIT))
  468. flags |= CXL_PE_64_BIT;
  469. cred = get_current_cred();
  470. if (uid_eq(cred->euid, GLOBAL_ROOT_UID))
  471. flags |= CXL_PE_PRIVILEGED_PROCESS;
  472. put_cred(cred);
  473. }
  474. elem->flags = cpu_to_be64(flags);
  475. elem->common.tid = cpu_to_be32(0); /* Unused */
  476. elem->common.pid = cpu_to_be32(pid);
  477. elem->common.csrp = cpu_to_be64(0); /* disable */
  478. elem->common.u.psl8.aurp0 = cpu_to_be64(0); /* disable */
  479. elem->common.u.psl8.aurp1 = cpu_to_be64(0); /* disable */
  480. cxl_prefault(ctx, wed);
  481. elem->common.u.psl8.sstp0 = cpu_to_be64(ctx->sstp0);
  482. elem->common.u.psl8.sstp1 = cpu_to_be64(ctx->sstp1);
  483. /*
  484. * Ensure we have at least one interrupt allocated to take faults for
  485. * kernel contexts that may not have allocated any AFU IRQs at all:
  486. */
  487. if (ctx->irqs.range[0] == 0) {
  488. rc = afu_register_irqs(ctx, 0);
  489. if (rc)
  490. goto out_free;
  491. }
  492. for (r = 0; r < CXL_IRQ_RANGES; r++) {
  493. for (i = 0; i < ctx->irqs.range[r]; i++) {
  494. if (r == 0 && i == 0) {
  495. elem->pslVirtualIsn = cpu_to_be32(ctx->irqs.offset[0]);
  496. } else {
  497. idx = ctx->irqs.offset[r] + i - adapter->guest->irq_base_offset;
  498. elem->applicationVirtualIsnBitmap[idx / 8] |= 0x80 >> (idx % 8);
  499. }
  500. }
  501. }
  502. elem->common.amr = cpu_to_be64(amr);
  503. elem->common.wed = cpu_to_be64(wed);
  504. disable_afu_irqs(ctx);
  505. rc = cxl_h_attach_process(ctx->afu->guest->handle, elem,
  506. &ctx->process_token, &mmio_addr, &mmio_size);
  507. if (rc == H_SUCCESS) {
  508. if (ctx->master || !ctx->afu->pp_psa) {
  509. ctx->psn_phys = ctx->afu->psn_phys;
  510. ctx->psn_size = ctx->afu->adapter->ps_size;
  511. } else {
  512. ctx->psn_phys = mmio_addr;
  513. ctx->psn_size = mmio_size;
  514. }
  515. if (ctx->afu->pp_psa && mmio_size &&
  516. ctx->afu->pp_size == 0) {
  517. /*
  518. * There's no property in the device tree to read the
  519. * pp_size. We only find out at the 1st attach.
  520. * Compared to bare-metal, it is too late and we
  521. * should really lock here. However, on powerVM,
  522. * pp_size is really only used to display in /sys.
  523. * Being discussed with pHyp for their next release.
  524. */
  525. ctx->afu->pp_size = mmio_size;
  526. }
  527. /* from PAPR: process element is bytes 4-7 of process token */
  528. ctx->external_pe = ctx->process_token & 0xFFFFFFFF;
  529. pr_devel("CXL pe=%i is known as %i for pHyp, mmio_size=%#llx",
  530. ctx->pe, ctx->external_pe, ctx->psn_size);
  531. ctx->pe_inserted = true;
  532. enable_afu_irqs(ctx);
  533. }
  534. out_free:
  535. free_page((u64)elem);
  536. return rc;
  537. }
  538. static int guest_attach_process(struct cxl_context *ctx, bool kernel, u64 wed, u64 amr)
  539. {
  540. pr_devel("in %s\n", __func__);
  541. ctx->kernel = kernel;
  542. if (ctx->afu->current_mode == CXL_MODE_DIRECTED)
  543. return attach_afu_directed(ctx, wed, amr);
  544. /* dedicated mode not supported on FW840 */
  545. return -EINVAL;
  546. }
  547. static int detach_afu_directed(struct cxl_context *ctx)
  548. {
  549. if (!ctx->pe_inserted)
  550. return 0;
  551. if (cxl_h_detach_process(ctx->afu->guest->handle, ctx->process_token))
  552. return -1;
  553. return 0;
  554. }
  555. static int guest_detach_process(struct cxl_context *ctx)
  556. {
  557. pr_devel("in %s\n", __func__);
  558. trace_cxl_detach(ctx);
  559. if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
  560. return -EIO;
  561. if (ctx->afu->current_mode == CXL_MODE_DIRECTED)
  562. return detach_afu_directed(ctx);
  563. return -EINVAL;
  564. }
  565. static void guest_release_afu(struct device *dev)
  566. {
  567. struct cxl_afu *afu = to_cxl_afu(dev);
  568. pr_devel("%s\n", __func__);
  569. idr_destroy(&afu->contexts_idr);
  570. kfree(afu->guest);
  571. kfree(afu);
  572. }
  573. ssize_t cxl_guest_read_afu_vpd(struct cxl_afu *afu, void *buf, size_t len)
  574. {
  575. return guest_collect_vpd(NULL, afu, buf, len);
  576. }
  577. #define ERR_BUFF_MAX_COPY_SIZE PAGE_SIZE
  578. static ssize_t guest_afu_read_err_buffer(struct cxl_afu *afu, char *buf,
  579. loff_t off, size_t count)
  580. {
  581. void *tbuf = NULL;
  582. int rc = 0;
  583. tbuf = (void *) get_zeroed_page(GFP_KERNEL);
  584. if (!tbuf)
  585. return -ENOMEM;
  586. rc = cxl_h_get_afu_err(afu->guest->handle,
  587. off & 0x7,
  588. virt_to_phys(tbuf),
  589. count);
  590. if (rc)
  591. goto err;
  592. if (count > ERR_BUFF_MAX_COPY_SIZE)
  593. count = ERR_BUFF_MAX_COPY_SIZE - (off & 0x7);
  594. memcpy(buf, tbuf, count);
  595. err:
  596. free_page((u64)tbuf);
  597. return rc;
  598. }
  599. static int guest_afu_check_and_enable(struct cxl_afu *afu)
  600. {
  601. return 0;
  602. }
  603. static bool guest_support_attributes(const char *attr_name,
  604. enum cxl_attrs type)
  605. {
  606. switch (type) {
  607. case CXL_ADAPTER_ATTRS:
  608. if ((strcmp(attr_name, "base_image") == 0) ||
  609. (strcmp(attr_name, "load_image_on_perst") == 0) ||
  610. (strcmp(attr_name, "perst_reloads_same_image") == 0) ||
  611. (strcmp(attr_name, "image_loaded") == 0))
  612. return false;
  613. break;
  614. case CXL_AFU_MASTER_ATTRS:
  615. if ((strcmp(attr_name, "pp_mmio_off") == 0))
  616. return false;
  617. break;
  618. case CXL_AFU_ATTRS:
  619. break;
  620. default:
  621. break;
  622. }
  623. return true;
  624. }
  625. static int activate_afu_directed(struct cxl_afu *afu)
  626. {
  627. int rc;
  628. dev_info(&afu->dev, "Activating AFU(%d) directed mode\n", afu->slice);
  629. afu->current_mode = CXL_MODE_DIRECTED;
  630. afu->num_procs = afu->max_procs_virtualised;
  631. if ((rc = cxl_chardev_m_afu_add(afu)))
  632. return rc;
  633. if ((rc = cxl_sysfs_afu_m_add(afu)))
  634. goto err;
  635. if ((rc = cxl_chardev_s_afu_add(afu)))
  636. goto err1;
  637. return 0;
  638. err1:
  639. cxl_sysfs_afu_m_remove(afu);
  640. err:
  641. cxl_chardev_afu_remove(afu);
  642. return rc;
  643. }
  644. static int guest_afu_activate_mode(struct cxl_afu *afu, int mode)
  645. {
  646. if (!mode)
  647. return 0;
  648. if (!(mode & afu->modes_supported))
  649. return -EINVAL;
  650. if (mode == CXL_MODE_DIRECTED)
  651. return activate_afu_directed(afu);
  652. if (mode == CXL_MODE_DEDICATED)
  653. dev_err(&afu->dev, "Dedicated mode not supported\n");
  654. return -EINVAL;
  655. }
  656. static int deactivate_afu_directed(struct cxl_afu *afu)
  657. {
  658. dev_info(&afu->dev, "Deactivating AFU(%d) directed mode\n", afu->slice);
  659. afu->current_mode = 0;
  660. afu->num_procs = 0;
  661. cxl_sysfs_afu_m_remove(afu);
  662. cxl_chardev_afu_remove(afu);
  663. cxl_ops->afu_reset(afu);
  664. return 0;
  665. }
  666. static int guest_afu_deactivate_mode(struct cxl_afu *afu, int mode)
  667. {
  668. if (!mode)
  669. return 0;
  670. if (!(mode & afu->modes_supported))
  671. return -EINVAL;
  672. if (mode == CXL_MODE_DIRECTED)
  673. return deactivate_afu_directed(afu);
  674. return 0;
  675. }
  676. static int guest_afu_reset(struct cxl_afu *afu)
  677. {
  678. pr_devel("AFU(%d) reset request\n", afu->slice);
  679. return cxl_h_reset_afu(afu->guest->handle);
  680. }
  681. static int guest_map_slice_regs(struct cxl_afu *afu)
  682. {
  683. if (!(afu->p2n_mmio = ioremap(afu->guest->p2n_phys, afu->guest->p2n_size))) {
  684. dev_err(&afu->dev, "Error mapping AFU(%d) MMIO regions\n",
  685. afu->slice);
  686. return -ENOMEM;
  687. }
  688. return 0;
  689. }
  690. static void guest_unmap_slice_regs(struct cxl_afu *afu)
  691. {
  692. if (afu->p2n_mmio)
  693. iounmap(afu->p2n_mmio);
  694. }
  695. static int afu_update_state(struct cxl_afu *afu)
  696. {
  697. int rc, cur_state;
  698. rc = afu_read_error_state(afu, &cur_state);
  699. if (rc)
  700. return rc;
  701. if (afu->guest->previous_state == cur_state)
  702. return 0;
  703. pr_devel("AFU(%d) update state to %#x\n", afu->slice, cur_state);
  704. switch (cur_state) {
  705. case H_STATE_NORMAL:
  706. afu->guest->previous_state = cur_state;
  707. break;
  708. case H_STATE_DISABLE:
  709. pci_error_handlers(afu, CXL_ERROR_DETECTED_EVENT,
  710. pci_channel_io_frozen);
  711. cxl_context_detach_all(afu);
  712. if ((rc = cxl_ops->afu_reset(afu)))
  713. pr_devel("reset hcall failed %d\n", rc);
  714. rc = afu_read_error_state(afu, &cur_state);
  715. if (!rc && cur_state == H_STATE_NORMAL) {
  716. pci_error_handlers(afu, CXL_SLOT_RESET_EVENT,
  717. pci_channel_io_normal);
  718. pci_error_handlers(afu, CXL_RESUME_EVENT, 0);
  719. }
  720. afu->guest->previous_state = 0;
  721. break;
  722. case H_STATE_TEMP_UNAVAILABLE:
  723. afu->guest->previous_state = cur_state;
  724. break;
  725. case H_STATE_PERM_UNAVAILABLE:
  726. dev_err(&afu->dev, "AFU is in permanent error state\n");
  727. pci_error_handlers(afu, CXL_ERROR_DETECTED_EVENT,
  728. pci_channel_io_perm_failure);
  729. afu->guest->previous_state = cur_state;
  730. break;
  731. default:
  732. pr_err("Unexpected AFU(%d) error state: %#x\n",
  733. afu->slice, cur_state);
  734. return -EINVAL;
  735. }
  736. return rc;
  737. }
  738. static void afu_handle_errstate(struct work_struct *work)
  739. {
  740. struct cxl_afu_guest *afu_guest =
  741. container_of(to_delayed_work(work), struct cxl_afu_guest, work_err);
  742. if (!afu_update_state(afu_guest->parent) &&
  743. afu_guest->previous_state == H_STATE_PERM_UNAVAILABLE)
  744. return;
  745. if (afu_guest->handle_err)
  746. schedule_delayed_work(&afu_guest->work_err,
  747. msecs_to_jiffies(3000));
  748. }
  749. static bool guest_link_ok(struct cxl *cxl, struct cxl_afu *afu)
  750. {
  751. int state;
  752. if (afu && (!afu_read_error_state(afu, &state))) {
  753. if (state == H_STATE_NORMAL)
  754. return true;
  755. }
  756. return false;
  757. }
  758. static int afu_properties_look_ok(struct cxl_afu *afu)
  759. {
  760. if (afu->pp_irqs < 0) {
  761. dev_err(&afu->dev, "Unexpected per-process minimum interrupt value\n");
  762. return -EINVAL;
  763. }
  764. if (afu->max_procs_virtualised < 1) {
  765. dev_err(&afu->dev, "Unexpected max number of processes virtualised value\n");
  766. return -EINVAL;
  767. }
  768. return 0;
  769. }
  770. int cxl_guest_init_afu(struct cxl *adapter, int slice, struct device_node *afu_np)
  771. {
  772. struct cxl_afu *afu;
  773. bool free = true;
  774. int rc;
  775. pr_devel("in %s - AFU(%d)\n", __func__, slice);
  776. if (!(afu = cxl_alloc_afu(adapter, slice)))
  777. return -ENOMEM;
  778. if (!(afu->guest = kzalloc(sizeof(struct cxl_afu_guest), GFP_KERNEL))) {
  779. kfree(afu);
  780. return -ENOMEM;
  781. }
  782. if ((rc = dev_set_name(&afu->dev, "afu%i.%i",
  783. adapter->adapter_num,
  784. slice)))
  785. goto err1;
  786. adapter->slices++;
  787. if ((rc = cxl_of_read_afu_handle(afu, afu_np)))
  788. goto err1;
  789. if ((rc = cxl_ops->afu_reset(afu)))
  790. goto err1;
  791. if ((rc = cxl_of_read_afu_properties(afu, afu_np)))
  792. goto err1;
  793. if ((rc = afu_properties_look_ok(afu)))
  794. goto err1;
  795. if ((rc = guest_map_slice_regs(afu)))
  796. goto err1;
  797. if ((rc = guest_register_serr_irq(afu)))
  798. goto err2;
  799. /*
  800. * After we call this function we must not free the afu directly, even
  801. * if it returns an error!
  802. */
  803. if ((rc = cxl_register_afu(afu)))
  804. goto err_put1;
  805. if ((rc = cxl_sysfs_afu_add(afu)))
  806. goto err_put1;
  807. /*
  808. * pHyp doesn't expose the programming models supported by the
  809. * AFU. pHyp currently only supports directed mode. If it adds
  810. * dedicated mode later, this version of cxl has no way to
  811. * detect it. So we'll initialize the driver, but the first
  812. * attach will fail.
  813. * Being discussed with pHyp to do better (likely new property)
  814. */
  815. if (afu->max_procs_virtualised == 1)
  816. afu->modes_supported = CXL_MODE_DEDICATED;
  817. else
  818. afu->modes_supported = CXL_MODE_DIRECTED;
  819. if ((rc = cxl_afu_select_best_mode(afu)))
  820. goto err_put2;
  821. adapter->afu[afu->slice] = afu;
  822. afu->enabled = true;
  823. /*
  824. * wake up the cpu periodically to check the state
  825. * of the AFU using "afu" stored in the guest structure.
  826. */
  827. afu->guest->parent = afu;
  828. afu->guest->handle_err = true;
  829. INIT_DELAYED_WORK(&afu->guest->work_err, afu_handle_errstate);
  830. schedule_delayed_work(&afu->guest->work_err, msecs_to_jiffies(1000));
  831. if ((rc = cxl_pci_vphb_add(afu)))
  832. dev_info(&afu->dev, "Can't register vPHB\n");
  833. return 0;
  834. err_put2:
  835. cxl_sysfs_afu_remove(afu);
  836. err_put1:
  837. device_unregister(&afu->dev);
  838. free = false;
  839. guest_release_serr_irq(afu);
  840. err2:
  841. guest_unmap_slice_regs(afu);
  842. err1:
  843. if (free) {
  844. kfree(afu->guest);
  845. kfree(afu);
  846. }
  847. return rc;
  848. }
  849. void cxl_guest_remove_afu(struct cxl_afu *afu)
  850. {
  851. pr_devel("in %s - AFU(%d)\n", __func__, afu->slice);
  852. if (!afu)
  853. return;
  854. /* flush and stop pending job */
  855. afu->guest->handle_err = false;
  856. flush_delayed_work(&afu->guest->work_err);
  857. cxl_pci_vphb_remove(afu);
  858. cxl_sysfs_afu_remove(afu);
  859. spin_lock(&afu->adapter->afu_list_lock);
  860. afu->adapter->afu[afu->slice] = NULL;
  861. spin_unlock(&afu->adapter->afu_list_lock);
  862. cxl_context_detach_all(afu);
  863. cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
  864. guest_release_serr_irq(afu);
  865. guest_unmap_slice_regs(afu);
  866. device_unregister(&afu->dev);
  867. }
  868. static void free_adapter(struct cxl *adapter)
  869. {
  870. struct irq_avail *cur;
  871. int i;
  872. if (adapter->guest) {
  873. if (adapter->guest->irq_avail) {
  874. for (i = 0; i < adapter->guest->irq_nranges; i++) {
  875. cur = &adapter->guest->irq_avail[i];
  876. kfree(cur->bitmap);
  877. }
  878. kfree(adapter->guest->irq_avail);
  879. }
  880. kfree(adapter->guest->status);
  881. kfree(adapter->guest);
  882. }
  883. cxl_remove_adapter_nr(adapter);
  884. kfree(adapter);
  885. }
  886. static int properties_look_ok(struct cxl *adapter)
  887. {
  888. /* The absence of this property means that the operational
  889. * status is unknown or okay
  890. */
  891. if (strlen(adapter->guest->status) &&
  892. strcmp(adapter->guest->status, "okay")) {
  893. pr_err("ABORTING:Bad operational status of the device\n");
  894. return -EINVAL;
  895. }
  896. return 0;
  897. }
  898. ssize_t cxl_guest_read_adapter_vpd(struct cxl *adapter, void *buf, size_t len)
  899. {
  900. return guest_collect_vpd(adapter, NULL, buf, len);
  901. }
  902. void cxl_guest_remove_adapter(struct cxl *adapter)
  903. {
  904. pr_devel("in %s\n", __func__);
  905. cxl_sysfs_adapter_remove(adapter);
  906. cxl_guest_remove_chardev(adapter);
  907. device_unregister(&adapter->dev);
  908. }
  909. static void release_adapter(struct device *dev)
  910. {
  911. free_adapter(to_cxl_adapter(dev));
  912. }
  913. struct cxl *cxl_guest_init_adapter(struct device_node *np, struct platform_device *pdev)
  914. {
  915. struct cxl *adapter;
  916. bool free = true;
  917. int rc;
  918. if (!(adapter = cxl_alloc_adapter()))
  919. return ERR_PTR(-ENOMEM);
  920. if (!(adapter->guest = kzalloc(sizeof(struct cxl_guest), GFP_KERNEL))) {
  921. free_adapter(adapter);
  922. return ERR_PTR(-ENOMEM);
  923. }
  924. adapter->slices = 0;
  925. adapter->guest->pdev = pdev;
  926. adapter->dev.parent = &pdev->dev;
  927. adapter->dev.release = release_adapter;
  928. dev_set_drvdata(&pdev->dev, adapter);
  929. /*
  930. * Hypervisor controls PSL timebase initialization (p1 register).
  931. * On FW840, PSL is initialized.
  932. */
  933. adapter->psl_timebase_synced = true;
  934. if ((rc = cxl_of_read_adapter_handle(adapter, np)))
  935. goto err1;
  936. if ((rc = cxl_of_read_adapter_properties(adapter, np)))
  937. goto err1;
  938. if ((rc = properties_look_ok(adapter)))
  939. goto err1;
  940. if ((rc = cxl_guest_add_chardev(adapter)))
  941. goto err1;
  942. /*
  943. * After we call this function we must not free the adapter directly,
  944. * even if it returns an error!
  945. */
  946. if ((rc = cxl_register_adapter(adapter)))
  947. goto err_put1;
  948. if ((rc = cxl_sysfs_adapter_add(adapter)))
  949. goto err_put1;
  950. /* release the context lock as the adapter is configured */
  951. cxl_adapter_context_unlock(adapter);
  952. return adapter;
  953. err_put1:
  954. device_unregister(&adapter->dev);
  955. free = false;
  956. cxl_guest_remove_chardev(adapter);
  957. err1:
  958. if (free)
  959. free_adapter(adapter);
  960. return ERR_PTR(rc);
  961. }
  962. void cxl_guest_reload_module(struct cxl *adapter)
  963. {
  964. struct platform_device *pdev;
  965. pdev = adapter->guest->pdev;
  966. cxl_guest_remove_adapter(adapter);
  967. cxl_of_probe(pdev);
  968. }
  969. const struct cxl_backend_ops cxl_guest_ops = {
  970. .module = THIS_MODULE,
  971. .adapter_reset = guest_reset,
  972. .alloc_one_irq = guest_alloc_one_irq,
  973. .release_one_irq = guest_release_one_irq,
  974. .alloc_irq_ranges = guest_alloc_irq_ranges,
  975. .release_irq_ranges = guest_release_irq_ranges,
  976. .setup_irq = NULL,
  977. .handle_psl_slice_error = guest_handle_psl_slice_error,
  978. .psl_interrupt = guest_psl_irq,
  979. .ack_irq = guest_ack_irq,
  980. .attach_process = guest_attach_process,
  981. .detach_process = guest_detach_process,
  982. .update_ivtes = NULL,
  983. .support_attributes = guest_support_attributes,
  984. .link_ok = guest_link_ok,
  985. .release_afu = guest_release_afu,
  986. .afu_read_err_buffer = guest_afu_read_err_buffer,
  987. .afu_check_and_enable = guest_afu_check_and_enable,
  988. .afu_activate_mode = guest_afu_activate_mode,
  989. .afu_deactivate_mode = guest_afu_deactivate_mode,
  990. .afu_reset = guest_afu_reset,
  991. .afu_cr_read8 = guest_afu_cr_read8,
  992. .afu_cr_read16 = guest_afu_cr_read16,
  993. .afu_cr_read32 = guest_afu_cr_read32,
  994. .afu_cr_read64 = guest_afu_cr_read64,
  995. .afu_cr_write8 = guest_afu_cr_write8,
  996. .afu_cr_write16 = guest_afu_cr_write16,
  997. .afu_cr_write32 = guest_afu_cr_write32,
  998. .read_adapter_vpd = cxl_guest_read_adapter_vpd,
  999. };