guest.c 28 KB

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