nfit.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104
  1. /*
  2. * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. */
  13. #include <linux/list_sort.h>
  14. #include <linux/libnvdimm.h>
  15. #include <linux/module.h>
  16. #include <linux/mutex.h>
  17. #include <linux/ndctl.h>
  18. #include <linux/delay.h>
  19. #include <linux/list.h>
  20. #include <linux/acpi.h>
  21. #include <linux/sort.h>
  22. #include <linux/pmem.h>
  23. #include <linux/io.h>
  24. #include <asm/cacheflush.h>
  25. #include "nfit.h"
  26. /*
  27. * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
  28. * irrelevant.
  29. */
  30. #include <linux/io-64-nonatomic-hi-lo.h>
  31. static bool force_enable_dimms;
  32. module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR);
  33. MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status");
  34. struct nfit_table_prev {
  35. struct list_head spas;
  36. struct list_head memdevs;
  37. struct list_head dcrs;
  38. struct list_head bdws;
  39. struct list_head idts;
  40. struct list_head flushes;
  41. };
  42. static u8 nfit_uuid[NFIT_UUID_MAX][16];
  43. const u8 *to_nfit_uuid(enum nfit_uuids id)
  44. {
  45. return nfit_uuid[id];
  46. }
  47. EXPORT_SYMBOL(to_nfit_uuid);
  48. static struct acpi_nfit_desc *to_acpi_nfit_desc(
  49. struct nvdimm_bus_descriptor *nd_desc)
  50. {
  51. return container_of(nd_desc, struct acpi_nfit_desc, nd_desc);
  52. }
  53. static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc)
  54. {
  55. struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
  56. /*
  57. * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
  58. * acpi_device.
  59. */
  60. if (!nd_desc->provider_name
  61. || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0)
  62. return NULL;
  63. return to_acpi_device(acpi_desc->dev);
  64. }
  65. static int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc,
  66. struct nvdimm *nvdimm, unsigned int cmd, void *buf,
  67. unsigned int buf_len)
  68. {
  69. struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
  70. const struct nd_cmd_desc *desc = NULL;
  71. union acpi_object in_obj, in_buf, *out_obj;
  72. struct device *dev = acpi_desc->dev;
  73. const char *cmd_name, *dimm_name;
  74. unsigned long dsm_mask;
  75. acpi_handle handle;
  76. const u8 *uuid;
  77. u32 offset;
  78. int rc, i;
  79. if (nvdimm) {
  80. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  81. struct acpi_device *adev = nfit_mem->adev;
  82. if (!adev)
  83. return -ENOTTY;
  84. dimm_name = nvdimm_name(nvdimm);
  85. cmd_name = nvdimm_cmd_name(cmd);
  86. dsm_mask = nfit_mem->dsm_mask;
  87. desc = nd_cmd_dimm_desc(cmd);
  88. uuid = to_nfit_uuid(NFIT_DEV_DIMM);
  89. handle = adev->handle;
  90. } else {
  91. struct acpi_device *adev = to_acpi_dev(acpi_desc);
  92. cmd_name = nvdimm_bus_cmd_name(cmd);
  93. dsm_mask = nd_desc->dsm_mask;
  94. desc = nd_cmd_bus_desc(cmd);
  95. uuid = to_nfit_uuid(NFIT_DEV_BUS);
  96. handle = adev->handle;
  97. dimm_name = "bus";
  98. }
  99. if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
  100. return -ENOTTY;
  101. if (!test_bit(cmd, &dsm_mask))
  102. return -ENOTTY;
  103. in_obj.type = ACPI_TYPE_PACKAGE;
  104. in_obj.package.count = 1;
  105. in_obj.package.elements = &in_buf;
  106. in_buf.type = ACPI_TYPE_BUFFER;
  107. in_buf.buffer.pointer = buf;
  108. in_buf.buffer.length = 0;
  109. /* libnvdimm has already validated the input envelope */
  110. for (i = 0; i < desc->in_num; i++)
  111. in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
  112. i, buf);
  113. if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
  114. dev_dbg(dev, "%s:%s cmd: %s input length: %d\n", __func__,
  115. dimm_name, cmd_name, in_buf.buffer.length);
  116. print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4,
  117. 4, in_buf.buffer.pointer, min_t(u32, 128,
  118. in_buf.buffer.length), true);
  119. }
  120. out_obj = acpi_evaluate_dsm(handle, uuid, 1, cmd, &in_obj);
  121. if (!out_obj) {
  122. dev_dbg(dev, "%s:%s _DSM failed cmd: %s\n", __func__, dimm_name,
  123. cmd_name);
  124. return -EINVAL;
  125. }
  126. if (out_obj->package.type != ACPI_TYPE_BUFFER) {
  127. dev_dbg(dev, "%s:%s unexpected output object type cmd: %s type: %d\n",
  128. __func__, dimm_name, cmd_name, out_obj->type);
  129. rc = -EINVAL;
  130. goto out;
  131. }
  132. if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
  133. dev_dbg(dev, "%s:%s cmd: %s output length: %d\n", __func__,
  134. dimm_name, cmd_name, out_obj->buffer.length);
  135. print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4,
  136. 4, out_obj->buffer.pointer, min_t(u32, 128,
  137. out_obj->buffer.length), true);
  138. }
  139. for (i = 0, offset = 0; i < desc->out_num; i++) {
  140. u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
  141. (u32 *) out_obj->buffer.pointer);
  142. if (offset + out_size > out_obj->buffer.length) {
  143. dev_dbg(dev, "%s:%s output object underflow cmd: %s field: %d\n",
  144. __func__, dimm_name, cmd_name, i);
  145. break;
  146. }
  147. if (in_buf.buffer.length + offset + out_size > buf_len) {
  148. dev_dbg(dev, "%s:%s output overrun cmd: %s field: %d\n",
  149. __func__, dimm_name, cmd_name, i);
  150. rc = -ENXIO;
  151. goto out;
  152. }
  153. memcpy(buf + in_buf.buffer.length + offset,
  154. out_obj->buffer.pointer + offset, out_size);
  155. offset += out_size;
  156. }
  157. if (offset + in_buf.buffer.length < buf_len) {
  158. if (i >= 1) {
  159. /*
  160. * status valid, return the number of bytes left
  161. * unfilled in the output buffer
  162. */
  163. rc = buf_len - offset - in_buf.buffer.length;
  164. } else {
  165. dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
  166. __func__, dimm_name, cmd_name, buf_len,
  167. offset);
  168. rc = -ENXIO;
  169. }
  170. } else
  171. rc = 0;
  172. out:
  173. ACPI_FREE(out_obj);
  174. return rc;
  175. }
  176. static const char *spa_type_name(u16 type)
  177. {
  178. static const char *to_name[] = {
  179. [NFIT_SPA_VOLATILE] = "volatile",
  180. [NFIT_SPA_PM] = "pmem",
  181. [NFIT_SPA_DCR] = "dimm-control-region",
  182. [NFIT_SPA_BDW] = "block-data-window",
  183. [NFIT_SPA_VDISK] = "volatile-disk",
  184. [NFIT_SPA_VCD] = "volatile-cd",
  185. [NFIT_SPA_PDISK] = "persistent-disk",
  186. [NFIT_SPA_PCD] = "persistent-cd",
  187. };
  188. if (type > NFIT_SPA_PCD)
  189. return "unknown";
  190. return to_name[type];
  191. }
  192. static int nfit_spa_type(struct acpi_nfit_system_address *spa)
  193. {
  194. int i;
  195. for (i = 0; i < NFIT_UUID_MAX; i++)
  196. if (memcmp(to_nfit_uuid(i), spa->range_guid, 16) == 0)
  197. return i;
  198. return -1;
  199. }
  200. static bool add_spa(struct acpi_nfit_desc *acpi_desc,
  201. struct nfit_table_prev *prev,
  202. struct acpi_nfit_system_address *spa)
  203. {
  204. size_t length = min_t(size_t, sizeof(*spa), spa->header.length);
  205. struct device *dev = acpi_desc->dev;
  206. struct nfit_spa *nfit_spa;
  207. list_for_each_entry(nfit_spa, &prev->spas, list) {
  208. if (memcmp(nfit_spa->spa, spa, length) == 0) {
  209. list_move_tail(&nfit_spa->list, &acpi_desc->spas);
  210. return true;
  211. }
  212. }
  213. nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa), GFP_KERNEL);
  214. if (!nfit_spa)
  215. return false;
  216. INIT_LIST_HEAD(&nfit_spa->list);
  217. nfit_spa->spa = spa;
  218. list_add_tail(&nfit_spa->list, &acpi_desc->spas);
  219. dev_dbg(dev, "%s: spa index: %d type: %s\n", __func__,
  220. spa->range_index,
  221. spa_type_name(nfit_spa_type(spa)));
  222. return true;
  223. }
  224. static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
  225. struct nfit_table_prev *prev,
  226. struct acpi_nfit_memory_map *memdev)
  227. {
  228. size_t length = min_t(size_t, sizeof(*memdev), memdev->header.length);
  229. struct device *dev = acpi_desc->dev;
  230. struct nfit_memdev *nfit_memdev;
  231. list_for_each_entry(nfit_memdev, &prev->memdevs, list)
  232. if (memcmp(nfit_memdev->memdev, memdev, length) == 0) {
  233. list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
  234. return true;
  235. }
  236. nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev), GFP_KERNEL);
  237. if (!nfit_memdev)
  238. return false;
  239. INIT_LIST_HEAD(&nfit_memdev->list);
  240. nfit_memdev->memdev = memdev;
  241. list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
  242. dev_dbg(dev, "%s: memdev handle: %#x spa: %d dcr: %d\n",
  243. __func__, memdev->device_handle, memdev->range_index,
  244. memdev->region_index);
  245. return true;
  246. }
  247. static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
  248. struct nfit_table_prev *prev,
  249. struct acpi_nfit_control_region *dcr)
  250. {
  251. size_t length = min_t(size_t, sizeof(*dcr), dcr->header.length);
  252. struct device *dev = acpi_desc->dev;
  253. struct nfit_dcr *nfit_dcr;
  254. list_for_each_entry(nfit_dcr, &prev->dcrs, list)
  255. if (memcmp(nfit_dcr->dcr, dcr, length) == 0) {
  256. list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
  257. return true;
  258. }
  259. nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr), GFP_KERNEL);
  260. if (!nfit_dcr)
  261. return false;
  262. INIT_LIST_HEAD(&nfit_dcr->list);
  263. nfit_dcr->dcr = dcr;
  264. list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
  265. dev_dbg(dev, "%s: dcr index: %d windows: %d\n", __func__,
  266. dcr->region_index, dcr->windows);
  267. return true;
  268. }
  269. static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
  270. struct nfit_table_prev *prev,
  271. struct acpi_nfit_data_region *bdw)
  272. {
  273. size_t length = min_t(size_t, sizeof(*bdw), bdw->header.length);
  274. struct device *dev = acpi_desc->dev;
  275. struct nfit_bdw *nfit_bdw;
  276. list_for_each_entry(nfit_bdw, &prev->bdws, list)
  277. if (memcmp(nfit_bdw->bdw, bdw, length) == 0) {
  278. list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
  279. return true;
  280. }
  281. nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw), GFP_KERNEL);
  282. if (!nfit_bdw)
  283. return false;
  284. INIT_LIST_HEAD(&nfit_bdw->list);
  285. nfit_bdw->bdw = bdw;
  286. list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
  287. dev_dbg(dev, "%s: bdw dcr: %d windows: %d\n", __func__,
  288. bdw->region_index, bdw->windows);
  289. return true;
  290. }
  291. static bool add_idt(struct acpi_nfit_desc *acpi_desc,
  292. struct nfit_table_prev *prev,
  293. struct acpi_nfit_interleave *idt)
  294. {
  295. size_t length = min_t(size_t, sizeof(*idt), idt->header.length);
  296. struct device *dev = acpi_desc->dev;
  297. struct nfit_idt *nfit_idt;
  298. list_for_each_entry(nfit_idt, &prev->idts, list)
  299. if (memcmp(nfit_idt->idt, idt, length) == 0) {
  300. list_move_tail(&nfit_idt->list, &acpi_desc->idts);
  301. return true;
  302. }
  303. nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt), GFP_KERNEL);
  304. if (!nfit_idt)
  305. return false;
  306. INIT_LIST_HEAD(&nfit_idt->list);
  307. nfit_idt->idt = idt;
  308. list_add_tail(&nfit_idt->list, &acpi_desc->idts);
  309. dev_dbg(dev, "%s: idt index: %d num_lines: %d\n", __func__,
  310. idt->interleave_index, idt->line_count);
  311. return true;
  312. }
  313. static bool add_flush(struct acpi_nfit_desc *acpi_desc,
  314. struct nfit_table_prev *prev,
  315. struct acpi_nfit_flush_address *flush)
  316. {
  317. size_t length = min_t(size_t, sizeof(*flush), flush->header.length);
  318. struct device *dev = acpi_desc->dev;
  319. struct nfit_flush *nfit_flush;
  320. list_for_each_entry(nfit_flush, &prev->flushes, list)
  321. if (memcmp(nfit_flush->flush, flush, length) == 0) {
  322. list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
  323. return true;
  324. }
  325. nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush), GFP_KERNEL);
  326. if (!nfit_flush)
  327. return false;
  328. INIT_LIST_HEAD(&nfit_flush->list);
  329. nfit_flush->flush = flush;
  330. list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
  331. dev_dbg(dev, "%s: nfit_flush handle: %d hint_count: %d\n", __func__,
  332. flush->device_handle, flush->hint_count);
  333. return true;
  334. }
  335. static void *add_table(struct acpi_nfit_desc *acpi_desc,
  336. struct nfit_table_prev *prev, void *table, const void *end)
  337. {
  338. struct device *dev = acpi_desc->dev;
  339. struct acpi_nfit_header *hdr;
  340. void *err = ERR_PTR(-ENOMEM);
  341. if (table >= end)
  342. return NULL;
  343. hdr = table;
  344. if (!hdr->length) {
  345. dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
  346. hdr->type);
  347. return NULL;
  348. }
  349. switch (hdr->type) {
  350. case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
  351. if (!add_spa(acpi_desc, prev, table))
  352. return err;
  353. break;
  354. case ACPI_NFIT_TYPE_MEMORY_MAP:
  355. if (!add_memdev(acpi_desc, prev, table))
  356. return err;
  357. break;
  358. case ACPI_NFIT_TYPE_CONTROL_REGION:
  359. if (!add_dcr(acpi_desc, prev, table))
  360. return err;
  361. break;
  362. case ACPI_NFIT_TYPE_DATA_REGION:
  363. if (!add_bdw(acpi_desc, prev, table))
  364. return err;
  365. break;
  366. case ACPI_NFIT_TYPE_INTERLEAVE:
  367. if (!add_idt(acpi_desc, prev, table))
  368. return err;
  369. break;
  370. case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
  371. if (!add_flush(acpi_desc, prev, table))
  372. return err;
  373. break;
  374. case ACPI_NFIT_TYPE_SMBIOS:
  375. dev_dbg(dev, "%s: smbios\n", __func__);
  376. break;
  377. default:
  378. dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
  379. break;
  380. }
  381. return table + hdr->length;
  382. }
  383. static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
  384. struct nfit_mem *nfit_mem)
  385. {
  386. u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
  387. u16 dcr = nfit_mem->dcr->region_index;
  388. struct nfit_spa *nfit_spa;
  389. list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
  390. u16 range_index = nfit_spa->spa->range_index;
  391. int type = nfit_spa_type(nfit_spa->spa);
  392. struct nfit_memdev *nfit_memdev;
  393. if (type != NFIT_SPA_BDW)
  394. continue;
  395. list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
  396. if (nfit_memdev->memdev->range_index != range_index)
  397. continue;
  398. if (nfit_memdev->memdev->device_handle != device_handle)
  399. continue;
  400. if (nfit_memdev->memdev->region_index != dcr)
  401. continue;
  402. nfit_mem->spa_bdw = nfit_spa->spa;
  403. return;
  404. }
  405. }
  406. dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
  407. nfit_mem->spa_dcr->range_index);
  408. nfit_mem->bdw = NULL;
  409. }
  410. static int nfit_mem_add(struct acpi_nfit_desc *acpi_desc,
  411. struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
  412. {
  413. u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
  414. struct nfit_memdev *nfit_memdev;
  415. struct nfit_flush *nfit_flush;
  416. struct nfit_dcr *nfit_dcr;
  417. struct nfit_bdw *nfit_bdw;
  418. struct nfit_idt *nfit_idt;
  419. u16 idt_idx, range_index;
  420. list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
  421. if (nfit_dcr->dcr->region_index != dcr)
  422. continue;
  423. nfit_mem->dcr = nfit_dcr->dcr;
  424. break;
  425. }
  426. if (!nfit_mem->dcr) {
  427. dev_dbg(acpi_desc->dev, "SPA %d missing:%s%s\n",
  428. spa->range_index, __to_nfit_memdev(nfit_mem)
  429. ? "" : " MEMDEV", nfit_mem->dcr ? "" : " DCR");
  430. return -ENODEV;
  431. }
  432. /*
  433. * We've found enough to create an nvdimm, optionally
  434. * find an associated BDW
  435. */
  436. list_add(&nfit_mem->list, &acpi_desc->dimms);
  437. list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
  438. if (nfit_bdw->bdw->region_index != dcr)
  439. continue;
  440. nfit_mem->bdw = nfit_bdw->bdw;
  441. break;
  442. }
  443. if (!nfit_mem->bdw)
  444. return 0;
  445. nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
  446. if (!nfit_mem->spa_bdw)
  447. return 0;
  448. range_index = nfit_mem->spa_bdw->range_index;
  449. list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
  450. if (nfit_memdev->memdev->range_index != range_index ||
  451. nfit_memdev->memdev->region_index != dcr)
  452. continue;
  453. nfit_mem->memdev_bdw = nfit_memdev->memdev;
  454. idt_idx = nfit_memdev->memdev->interleave_index;
  455. list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
  456. if (nfit_idt->idt->interleave_index != idt_idx)
  457. continue;
  458. nfit_mem->idt_bdw = nfit_idt->idt;
  459. break;
  460. }
  461. list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
  462. if (nfit_flush->flush->device_handle !=
  463. nfit_memdev->memdev->device_handle)
  464. continue;
  465. nfit_mem->nfit_flush = nfit_flush;
  466. break;
  467. }
  468. break;
  469. }
  470. return 0;
  471. }
  472. static int nfit_mem_dcr_init(struct acpi_nfit_desc *acpi_desc,
  473. struct acpi_nfit_system_address *spa)
  474. {
  475. struct nfit_mem *nfit_mem, *found;
  476. struct nfit_memdev *nfit_memdev;
  477. int type = nfit_spa_type(spa);
  478. u16 dcr;
  479. switch (type) {
  480. case NFIT_SPA_DCR:
  481. case NFIT_SPA_PM:
  482. break;
  483. default:
  484. return 0;
  485. }
  486. list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
  487. int rc;
  488. if (nfit_memdev->memdev->range_index != spa->range_index)
  489. continue;
  490. found = NULL;
  491. dcr = nfit_memdev->memdev->region_index;
  492. list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
  493. if (__to_nfit_memdev(nfit_mem)->region_index == dcr) {
  494. found = nfit_mem;
  495. break;
  496. }
  497. if (found)
  498. nfit_mem = found;
  499. else {
  500. nfit_mem = devm_kzalloc(acpi_desc->dev,
  501. sizeof(*nfit_mem), GFP_KERNEL);
  502. if (!nfit_mem)
  503. return -ENOMEM;
  504. INIT_LIST_HEAD(&nfit_mem->list);
  505. }
  506. if (type == NFIT_SPA_DCR) {
  507. struct nfit_idt *nfit_idt;
  508. u16 idt_idx;
  509. /* multiple dimms may share a SPA when interleaved */
  510. nfit_mem->spa_dcr = spa;
  511. nfit_mem->memdev_dcr = nfit_memdev->memdev;
  512. idt_idx = nfit_memdev->memdev->interleave_index;
  513. list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
  514. if (nfit_idt->idt->interleave_index != idt_idx)
  515. continue;
  516. nfit_mem->idt_dcr = nfit_idt->idt;
  517. break;
  518. }
  519. } else {
  520. /*
  521. * A single dimm may belong to multiple SPA-PM
  522. * ranges, record at least one in addition to
  523. * any SPA-DCR range.
  524. */
  525. nfit_mem->memdev_pmem = nfit_memdev->memdev;
  526. }
  527. if (found)
  528. continue;
  529. rc = nfit_mem_add(acpi_desc, nfit_mem, spa);
  530. if (rc)
  531. return rc;
  532. }
  533. return 0;
  534. }
  535. static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
  536. {
  537. struct nfit_mem *a = container_of(_a, typeof(*a), list);
  538. struct nfit_mem *b = container_of(_b, typeof(*b), list);
  539. u32 handleA, handleB;
  540. handleA = __to_nfit_memdev(a)->device_handle;
  541. handleB = __to_nfit_memdev(b)->device_handle;
  542. if (handleA < handleB)
  543. return -1;
  544. else if (handleA > handleB)
  545. return 1;
  546. return 0;
  547. }
  548. static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
  549. {
  550. struct nfit_spa *nfit_spa;
  551. /*
  552. * For each SPA-DCR or SPA-PMEM address range find its
  553. * corresponding MEMDEV(s). From each MEMDEV find the
  554. * corresponding DCR. Then, if we're operating on a SPA-DCR,
  555. * try to find a SPA-BDW and a corresponding BDW that references
  556. * the DCR. Throw it all into an nfit_mem object. Note, that
  557. * BDWs are optional.
  558. */
  559. list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
  560. int rc;
  561. rc = nfit_mem_dcr_init(acpi_desc, nfit_spa->spa);
  562. if (rc)
  563. return rc;
  564. }
  565. list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
  566. return 0;
  567. }
  568. static ssize_t revision_show(struct device *dev,
  569. struct device_attribute *attr, char *buf)
  570. {
  571. struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
  572. struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
  573. struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
  574. return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
  575. }
  576. static DEVICE_ATTR_RO(revision);
  577. static struct attribute *acpi_nfit_attributes[] = {
  578. &dev_attr_revision.attr,
  579. NULL,
  580. };
  581. static struct attribute_group acpi_nfit_attribute_group = {
  582. .name = "nfit",
  583. .attrs = acpi_nfit_attributes,
  584. };
  585. const struct attribute_group *acpi_nfit_attribute_groups[] = {
  586. &nvdimm_bus_attribute_group,
  587. &acpi_nfit_attribute_group,
  588. NULL,
  589. };
  590. EXPORT_SYMBOL_GPL(acpi_nfit_attribute_groups);
  591. static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
  592. {
  593. struct nvdimm *nvdimm = to_nvdimm(dev);
  594. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  595. return __to_nfit_memdev(nfit_mem);
  596. }
  597. static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
  598. {
  599. struct nvdimm *nvdimm = to_nvdimm(dev);
  600. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  601. return nfit_mem->dcr;
  602. }
  603. static ssize_t handle_show(struct device *dev,
  604. struct device_attribute *attr, char *buf)
  605. {
  606. struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
  607. return sprintf(buf, "%#x\n", memdev->device_handle);
  608. }
  609. static DEVICE_ATTR_RO(handle);
  610. static ssize_t phys_id_show(struct device *dev,
  611. struct device_attribute *attr, char *buf)
  612. {
  613. struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
  614. return sprintf(buf, "%#x\n", memdev->physical_id);
  615. }
  616. static DEVICE_ATTR_RO(phys_id);
  617. static ssize_t vendor_show(struct device *dev,
  618. struct device_attribute *attr, char *buf)
  619. {
  620. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  621. return sprintf(buf, "%#x\n", dcr->vendor_id);
  622. }
  623. static DEVICE_ATTR_RO(vendor);
  624. static ssize_t rev_id_show(struct device *dev,
  625. struct device_attribute *attr, char *buf)
  626. {
  627. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  628. return sprintf(buf, "%#x\n", dcr->revision_id);
  629. }
  630. static DEVICE_ATTR_RO(rev_id);
  631. static ssize_t device_show(struct device *dev,
  632. struct device_attribute *attr, char *buf)
  633. {
  634. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  635. return sprintf(buf, "%#x\n", dcr->device_id);
  636. }
  637. static DEVICE_ATTR_RO(device);
  638. static ssize_t format_show(struct device *dev,
  639. struct device_attribute *attr, char *buf)
  640. {
  641. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  642. return sprintf(buf, "%#x\n", dcr->code);
  643. }
  644. static DEVICE_ATTR_RO(format);
  645. static ssize_t serial_show(struct device *dev,
  646. struct device_attribute *attr, char *buf)
  647. {
  648. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  649. return sprintf(buf, "%#x\n", dcr->serial_number);
  650. }
  651. static DEVICE_ATTR_RO(serial);
  652. static ssize_t flags_show(struct device *dev,
  653. struct device_attribute *attr, char *buf)
  654. {
  655. u16 flags = to_nfit_memdev(dev)->flags;
  656. return sprintf(buf, "%s%s%s%s%s\n",
  657. flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
  658. flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
  659. flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
  660. flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
  661. flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "");
  662. }
  663. static DEVICE_ATTR_RO(flags);
  664. static struct attribute *acpi_nfit_dimm_attributes[] = {
  665. &dev_attr_handle.attr,
  666. &dev_attr_phys_id.attr,
  667. &dev_attr_vendor.attr,
  668. &dev_attr_device.attr,
  669. &dev_attr_format.attr,
  670. &dev_attr_serial.attr,
  671. &dev_attr_rev_id.attr,
  672. &dev_attr_flags.attr,
  673. NULL,
  674. };
  675. static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
  676. struct attribute *a, int n)
  677. {
  678. struct device *dev = container_of(kobj, struct device, kobj);
  679. if (to_nfit_dcr(dev))
  680. return a->mode;
  681. else
  682. return 0;
  683. }
  684. static struct attribute_group acpi_nfit_dimm_attribute_group = {
  685. .name = "nfit",
  686. .attrs = acpi_nfit_dimm_attributes,
  687. .is_visible = acpi_nfit_dimm_attr_visible,
  688. };
  689. static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
  690. &nvdimm_attribute_group,
  691. &nd_device_attribute_group,
  692. &acpi_nfit_dimm_attribute_group,
  693. NULL,
  694. };
  695. static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
  696. u32 device_handle)
  697. {
  698. struct nfit_mem *nfit_mem;
  699. list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
  700. if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
  701. return nfit_mem->nvdimm;
  702. return NULL;
  703. }
  704. static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
  705. struct nfit_mem *nfit_mem, u32 device_handle)
  706. {
  707. struct acpi_device *adev, *adev_dimm;
  708. struct device *dev = acpi_desc->dev;
  709. const u8 *uuid = to_nfit_uuid(NFIT_DEV_DIMM);
  710. int i;
  711. nfit_mem->dsm_mask = acpi_desc->dimm_dsm_force_en;
  712. adev = to_acpi_dev(acpi_desc);
  713. if (!adev)
  714. return 0;
  715. adev_dimm = acpi_find_child_device(adev, device_handle, false);
  716. nfit_mem->adev = adev_dimm;
  717. if (!adev_dimm) {
  718. dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
  719. device_handle);
  720. return force_enable_dimms ? 0 : -ENODEV;
  721. }
  722. for (i = ND_CMD_SMART; i <= ND_CMD_VENDOR; i++)
  723. if (acpi_check_dsm(adev_dimm->handle, uuid, 1, 1ULL << i))
  724. set_bit(i, &nfit_mem->dsm_mask);
  725. return 0;
  726. }
  727. static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
  728. {
  729. struct nfit_mem *nfit_mem;
  730. int dimm_count = 0;
  731. list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
  732. struct nvdimm *nvdimm;
  733. unsigned long flags = 0;
  734. u32 device_handle;
  735. u16 mem_flags;
  736. int rc;
  737. device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
  738. nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
  739. if (nvdimm) {
  740. dimm_count++;
  741. continue;
  742. }
  743. if (nfit_mem->bdw && nfit_mem->memdev_pmem)
  744. flags |= NDD_ALIASING;
  745. mem_flags = __to_nfit_memdev(nfit_mem)->flags;
  746. if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
  747. flags |= NDD_UNARMED;
  748. rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
  749. if (rc)
  750. continue;
  751. nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
  752. acpi_nfit_dimm_attribute_groups,
  753. flags, &nfit_mem->dsm_mask);
  754. if (!nvdimm)
  755. return -ENOMEM;
  756. nfit_mem->nvdimm = nvdimm;
  757. dimm_count++;
  758. if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
  759. continue;
  760. dev_info(acpi_desc->dev, "%s flags:%s%s%s%s\n",
  761. nvdimm_name(nvdimm),
  762. mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
  763. mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
  764. mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
  765. mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "");
  766. }
  767. return nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
  768. }
  769. static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
  770. {
  771. struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
  772. const u8 *uuid = to_nfit_uuid(NFIT_DEV_BUS);
  773. struct acpi_device *adev;
  774. int i;
  775. nd_desc->dsm_mask = acpi_desc->bus_dsm_force_en;
  776. adev = to_acpi_dev(acpi_desc);
  777. if (!adev)
  778. return;
  779. for (i = ND_CMD_ARS_CAP; i <= ND_CMD_ARS_STATUS; i++)
  780. if (acpi_check_dsm(adev->handle, uuid, 1, 1ULL << i))
  781. set_bit(i, &nd_desc->dsm_mask);
  782. }
  783. static ssize_t range_index_show(struct device *dev,
  784. struct device_attribute *attr, char *buf)
  785. {
  786. struct nd_region *nd_region = to_nd_region(dev);
  787. struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
  788. return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
  789. }
  790. static DEVICE_ATTR_RO(range_index);
  791. static struct attribute *acpi_nfit_region_attributes[] = {
  792. &dev_attr_range_index.attr,
  793. NULL,
  794. };
  795. static struct attribute_group acpi_nfit_region_attribute_group = {
  796. .name = "nfit",
  797. .attrs = acpi_nfit_region_attributes,
  798. };
  799. static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
  800. &nd_region_attribute_group,
  801. &nd_mapping_attribute_group,
  802. &nd_device_attribute_group,
  803. &nd_numa_attribute_group,
  804. &acpi_nfit_region_attribute_group,
  805. NULL,
  806. };
  807. /* enough info to uniquely specify an interleave set */
  808. struct nfit_set_info {
  809. struct nfit_set_info_map {
  810. u64 region_offset;
  811. u32 serial_number;
  812. u32 pad;
  813. } mapping[0];
  814. };
  815. static size_t sizeof_nfit_set_info(int num_mappings)
  816. {
  817. return sizeof(struct nfit_set_info)
  818. + num_mappings * sizeof(struct nfit_set_info_map);
  819. }
  820. static int cmp_map(const void *m0, const void *m1)
  821. {
  822. const struct nfit_set_info_map *map0 = m0;
  823. const struct nfit_set_info_map *map1 = m1;
  824. return memcmp(&map0->region_offset, &map1->region_offset,
  825. sizeof(u64));
  826. }
  827. /* Retrieve the nth entry referencing this spa */
  828. static struct acpi_nfit_memory_map *memdev_from_spa(
  829. struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
  830. {
  831. struct nfit_memdev *nfit_memdev;
  832. list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
  833. if (nfit_memdev->memdev->range_index == range_index)
  834. if (n-- == 0)
  835. return nfit_memdev->memdev;
  836. return NULL;
  837. }
  838. static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
  839. struct nd_region_desc *ndr_desc,
  840. struct acpi_nfit_system_address *spa)
  841. {
  842. int i, spa_type = nfit_spa_type(spa);
  843. struct device *dev = acpi_desc->dev;
  844. struct nd_interleave_set *nd_set;
  845. u16 nr = ndr_desc->num_mappings;
  846. struct nfit_set_info *info;
  847. if (spa_type == NFIT_SPA_PM || spa_type == NFIT_SPA_VOLATILE)
  848. /* pass */;
  849. else
  850. return 0;
  851. nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
  852. if (!nd_set)
  853. return -ENOMEM;
  854. info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
  855. if (!info)
  856. return -ENOMEM;
  857. for (i = 0; i < nr; i++) {
  858. struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i];
  859. struct nfit_set_info_map *map = &info->mapping[i];
  860. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  861. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  862. struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
  863. spa->range_index, i);
  864. if (!memdev || !nfit_mem->dcr) {
  865. dev_err(dev, "%s: failed to find DCR\n", __func__);
  866. return -ENODEV;
  867. }
  868. map->region_offset = memdev->region_offset;
  869. map->serial_number = nfit_mem->dcr->serial_number;
  870. }
  871. sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
  872. cmp_map, NULL);
  873. nd_set->cookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
  874. ndr_desc->nd_set = nd_set;
  875. devm_kfree(dev, info);
  876. return 0;
  877. }
  878. static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
  879. {
  880. struct acpi_nfit_interleave *idt = mmio->idt;
  881. u32 sub_line_offset, line_index, line_offset;
  882. u64 line_no, table_skip_count, table_offset;
  883. line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
  884. table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
  885. line_offset = idt->line_offset[line_index]
  886. * mmio->line_size;
  887. table_offset = table_skip_count * mmio->table_size;
  888. return mmio->base_offset + line_offset + table_offset + sub_line_offset;
  889. }
  890. static void wmb_blk(struct nfit_blk *nfit_blk)
  891. {
  892. if (nfit_blk->nvdimm_flush) {
  893. /*
  894. * The first wmb() is needed to 'sfence' all previous writes
  895. * such that they are architecturally visible for the platform
  896. * buffer flush. Note that we've already arranged for pmem
  897. * writes to avoid the cache via arch_memcpy_to_pmem(). The
  898. * final wmb() ensures ordering for the NVDIMM flush write.
  899. */
  900. wmb();
  901. writeq(1, nfit_blk->nvdimm_flush);
  902. wmb();
  903. } else
  904. wmb_pmem();
  905. }
  906. static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
  907. {
  908. struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
  909. u64 offset = nfit_blk->stat_offset + mmio->size * bw;
  910. if (mmio->num_lines)
  911. offset = to_interleave_offset(offset, mmio);
  912. return readl(mmio->addr.base + offset);
  913. }
  914. static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
  915. resource_size_t dpa, unsigned int len, unsigned int write)
  916. {
  917. u64 cmd, offset;
  918. struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
  919. enum {
  920. BCW_OFFSET_MASK = (1ULL << 48)-1,
  921. BCW_LEN_SHIFT = 48,
  922. BCW_LEN_MASK = (1ULL << 8) - 1,
  923. BCW_CMD_SHIFT = 56,
  924. };
  925. cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
  926. len = len >> L1_CACHE_SHIFT;
  927. cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
  928. cmd |= ((u64) write) << BCW_CMD_SHIFT;
  929. offset = nfit_blk->cmd_offset + mmio->size * bw;
  930. if (mmio->num_lines)
  931. offset = to_interleave_offset(offset, mmio);
  932. writeq(cmd, mmio->addr.base + offset);
  933. wmb_blk(nfit_blk);
  934. if (nfit_blk->dimm_flags & ND_BLK_DCR_LATCH)
  935. readq(mmio->addr.base + offset);
  936. }
  937. static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
  938. resource_size_t dpa, void *iobuf, size_t len, int rw,
  939. unsigned int lane)
  940. {
  941. struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
  942. unsigned int copied = 0;
  943. u64 base_offset;
  944. int rc;
  945. base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
  946. + lane * mmio->size;
  947. write_blk_ctl(nfit_blk, lane, dpa, len, rw);
  948. while (len) {
  949. unsigned int c;
  950. u64 offset;
  951. if (mmio->num_lines) {
  952. u32 line_offset;
  953. offset = to_interleave_offset(base_offset + copied,
  954. mmio);
  955. div_u64_rem(offset, mmio->line_size, &line_offset);
  956. c = min_t(size_t, len, mmio->line_size - line_offset);
  957. } else {
  958. offset = base_offset + nfit_blk->bdw_offset;
  959. c = len;
  960. }
  961. if (rw)
  962. memcpy_to_pmem(mmio->addr.aperture + offset,
  963. iobuf + copied, c);
  964. else {
  965. if (nfit_blk->dimm_flags & ND_BLK_READ_FLUSH)
  966. mmio_flush_range((void __force *)
  967. mmio->addr.aperture + offset, c);
  968. memcpy_from_pmem(iobuf + copied,
  969. mmio->addr.aperture + offset, c);
  970. }
  971. copied += c;
  972. len -= c;
  973. }
  974. if (rw)
  975. wmb_blk(nfit_blk);
  976. rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
  977. return rc;
  978. }
  979. static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
  980. resource_size_t dpa, void *iobuf, u64 len, int rw)
  981. {
  982. struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
  983. struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
  984. struct nd_region *nd_region = nfit_blk->nd_region;
  985. unsigned int lane, copied = 0;
  986. int rc = 0;
  987. lane = nd_region_acquire_lane(nd_region);
  988. while (len) {
  989. u64 c = min(len, mmio->size);
  990. rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
  991. iobuf + copied, c, rw, lane);
  992. if (rc)
  993. break;
  994. copied += c;
  995. len -= c;
  996. }
  997. nd_region_release_lane(nd_region, lane);
  998. return rc;
  999. }
  1000. static void nfit_spa_mapping_release(struct kref *kref)
  1001. {
  1002. struct nfit_spa_mapping *spa_map = to_spa_map(kref);
  1003. struct acpi_nfit_system_address *spa = spa_map->spa;
  1004. struct acpi_nfit_desc *acpi_desc = spa_map->acpi_desc;
  1005. WARN_ON(!mutex_is_locked(&acpi_desc->spa_map_mutex));
  1006. dev_dbg(acpi_desc->dev, "%s: SPA%d\n", __func__, spa->range_index);
  1007. if (spa_map->type == SPA_MAP_APERTURE)
  1008. memunmap((void __force *)spa_map->addr.aperture);
  1009. else
  1010. iounmap(spa_map->addr.base);
  1011. release_mem_region(spa->address, spa->length);
  1012. list_del(&spa_map->list);
  1013. kfree(spa_map);
  1014. }
  1015. static struct nfit_spa_mapping *find_spa_mapping(
  1016. struct acpi_nfit_desc *acpi_desc,
  1017. struct acpi_nfit_system_address *spa)
  1018. {
  1019. struct nfit_spa_mapping *spa_map;
  1020. WARN_ON(!mutex_is_locked(&acpi_desc->spa_map_mutex));
  1021. list_for_each_entry(spa_map, &acpi_desc->spa_maps, list)
  1022. if (spa_map->spa == spa)
  1023. return spa_map;
  1024. return NULL;
  1025. }
  1026. static void nfit_spa_unmap(struct acpi_nfit_desc *acpi_desc,
  1027. struct acpi_nfit_system_address *spa)
  1028. {
  1029. struct nfit_spa_mapping *spa_map;
  1030. mutex_lock(&acpi_desc->spa_map_mutex);
  1031. spa_map = find_spa_mapping(acpi_desc, spa);
  1032. if (spa_map)
  1033. kref_put(&spa_map->kref, nfit_spa_mapping_release);
  1034. mutex_unlock(&acpi_desc->spa_map_mutex);
  1035. }
  1036. static void __iomem *__nfit_spa_map(struct acpi_nfit_desc *acpi_desc,
  1037. struct acpi_nfit_system_address *spa, enum spa_map_type type)
  1038. {
  1039. resource_size_t start = spa->address;
  1040. resource_size_t n = spa->length;
  1041. struct nfit_spa_mapping *spa_map;
  1042. struct resource *res;
  1043. WARN_ON(!mutex_is_locked(&acpi_desc->spa_map_mutex));
  1044. spa_map = find_spa_mapping(acpi_desc, spa);
  1045. if (spa_map) {
  1046. kref_get(&spa_map->kref);
  1047. return spa_map->addr.base;
  1048. }
  1049. spa_map = kzalloc(sizeof(*spa_map), GFP_KERNEL);
  1050. if (!spa_map)
  1051. return NULL;
  1052. INIT_LIST_HEAD(&spa_map->list);
  1053. spa_map->spa = spa;
  1054. kref_init(&spa_map->kref);
  1055. spa_map->acpi_desc = acpi_desc;
  1056. res = request_mem_region(start, n, dev_name(acpi_desc->dev));
  1057. if (!res)
  1058. goto err_mem;
  1059. spa_map->type = type;
  1060. if (type == SPA_MAP_APERTURE)
  1061. spa_map->addr.aperture = (void __pmem *)memremap(start, n,
  1062. ARCH_MEMREMAP_PMEM);
  1063. else
  1064. spa_map->addr.base = ioremap_nocache(start, n);
  1065. if (!spa_map->addr.base)
  1066. goto err_map;
  1067. list_add_tail(&spa_map->list, &acpi_desc->spa_maps);
  1068. return spa_map->addr.base;
  1069. err_map:
  1070. release_mem_region(start, n);
  1071. err_mem:
  1072. kfree(spa_map);
  1073. return NULL;
  1074. }
  1075. /**
  1076. * nfit_spa_map - interleave-aware managed-mappings of acpi_nfit_system_address ranges
  1077. * @nvdimm_bus: NFIT-bus that provided the spa table entry
  1078. * @nfit_spa: spa table to map
  1079. * @type: aperture or control region
  1080. *
  1081. * In the case where block-data-window apertures and
  1082. * dimm-control-regions are interleaved they will end up sharing a
  1083. * single request_mem_region() + ioremap() for the address range. In
  1084. * the style of devm nfit_spa_map() mappings are automatically dropped
  1085. * when all region devices referencing the same mapping are disabled /
  1086. * unbound.
  1087. */
  1088. static void __iomem *nfit_spa_map(struct acpi_nfit_desc *acpi_desc,
  1089. struct acpi_nfit_system_address *spa, enum spa_map_type type)
  1090. {
  1091. void __iomem *iomem;
  1092. mutex_lock(&acpi_desc->spa_map_mutex);
  1093. iomem = __nfit_spa_map(acpi_desc, spa, type);
  1094. mutex_unlock(&acpi_desc->spa_map_mutex);
  1095. return iomem;
  1096. }
  1097. static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
  1098. struct acpi_nfit_interleave *idt, u16 interleave_ways)
  1099. {
  1100. if (idt) {
  1101. mmio->num_lines = idt->line_count;
  1102. mmio->line_size = idt->line_size;
  1103. if (interleave_ways == 0)
  1104. return -ENXIO;
  1105. mmio->table_size = mmio->num_lines * interleave_ways
  1106. * mmio->line_size;
  1107. }
  1108. return 0;
  1109. }
  1110. static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
  1111. struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
  1112. {
  1113. struct nd_cmd_dimm_flags flags;
  1114. int rc;
  1115. memset(&flags, 0, sizeof(flags));
  1116. rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
  1117. sizeof(flags));
  1118. if (rc >= 0 && flags.status == 0)
  1119. nfit_blk->dimm_flags = flags.flags;
  1120. else if (rc == -ENOTTY) {
  1121. /* fall back to a conservative default */
  1122. nfit_blk->dimm_flags = ND_BLK_DCR_LATCH | ND_BLK_READ_FLUSH;
  1123. rc = 0;
  1124. } else
  1125. rc = -ENXIO;
  1126. return rc;
  1127. }
  1128. static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
  1129. struct device *dev)
  1130. {
  1131. struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
  1132. struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
  1133. struct nd_blk_region *ndbr = to_nd_blk_region(dev);
  1134. struct nfit_flush *nfit_flush;
  1135. struct nfit_blk_mmio *mmio;
  1136. struct nfit_blk *nfit_blk;
  1137. struct nfit_mem *nfit_mem;
  1138. struct nvdimm *nvdimm;
  1139. int rc;
  1140. nvdimm = nd_blk_region_to_dimm(ndbr);
  1141. nfit_mem = nvdimm_provider_data(nvdimm);
  1142. if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
  1143. dev_dbg(dev, "%s: missing%s%s%s\n", __func__,
  1144. nfit_mem ? "" : " nfit_mem",
  1145. (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
  1146. (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
  1147. return -ENXIO;
  1148. }
  1149. nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
  1150. if (!nfit_blk)
  1151. return -ENOMEM;
  1152. nd_blk_region_set_provider_data(ndbr, nfit_blk);
  1153. nfit_blk->nd_region = to_nd_region(dev);
  1154. /* map block aperture memory */
  1155. nfit_blk->bdw_offset = nfit_mem->bdw->offset;
  1156. mmio = &nfit_blk->mmio[BDW];
  1157. mmio->addr.base = nfit_spa_map(acpi_desc, nfit_mem->spa_bdw,
  1158. SPA_MAP_APERTURE);
  1159. if (!mmio->addr.base) {
  1160. dev_dbg(dev, "%s: %s failed to map bdw\n", __func__,
  1161. nvdimm_name(nvdimm));
  1162. return -ENOMEM;
  1163. }
  1164. mmio->size = nfit_mem->bdw->size;
  1165. mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
  1166. mmio->idt = nfit_mem->idt_bdw;
  1167. mmio->spa = nfit_mem->spa_bdw;
  1168. rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
  1169. nfit_mem->memdev_bdw->interleave_ways);
  1170. if (rc) {
  1171. dev_dbg(dev, "%s: %s failed to init bdw interleave\n",
  1172. __func__, nvdimm_name(nvdimm));
  1173. return rc;
  1174. }
  1175. /* map block control memory */
  1176. nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
  1177. nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
  1178. mmio = &nfit_blk->mmio[DCR];
  1179. mmio->addr.base = nfit_spa_map(acpi_desc, nfit_mem->spa_dcr,
  1180. SPA_MAP_CONTROL);
  1181. if (!mmio->addr.base) {
  1182. dev_dbg(dev, "%s: %s failed to map dcr\n", __func__,
  1183. nvdimm_name(nvdimm));
  1184. return -ENOMEM;
  1185. }
  1186. mmio->size = nfit_mem->dcr->window_size;
  1187. mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
  1188. mmio->idt = nfit_mem->idt_dcr;
  1189. mmio->spa = nfit_mem->spa_dcr;
  1190. rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
  1191. nfit_mem->memdev_dcr->interleave_ways);
  1192. if (rc) {
  1193. dev_dbg(dev, "%s: %s failed to init dcr interleave\n",
  1194. __func__, nvdimm_name(nvdimm));
  1195. return rc;
  1196. }
  1197. rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
  1198. if (rc < 0) {
  1199. dev_dbg(dev, "%s: %s failed get DIMM flags\n",
  1200. __func__, nvdimm_name(nvdimm));
  1201. return rc;
  1202. }
  1203. nfit_flush = nfit_mem->nfit_flush;
  1204. if (nfit_flush && nfit_flush->flush->hint_count != 0) {
  1205. nfit_blk->nvdimm_flush = devm_ioremap_nocache(dev,
  1206. nfit_flush->flush->hint_address[0], 8);
  1207. if (!nfit_blk->nvdimm_flush)
  1208. return -ENOMEM;
  1209. }
  1210. if (!arch_has_wmb_pmem() && !nfit_blk->nvdimm_flush)
  1211. dev_warn(dev, "unable to guarantee persistence of writes\n");
  1212. if (mmio->line_size == 0)
  1213. return 0;
  1214. if ((u32) nfit_blk->cmd_offset % mmio->line_size
  1215. + 8 > mmio->line_size) {
  1216. dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
  1217. return -ENXIO;
  1218. } else if ((u32) nfit_blk->stat_offset % mmio->line_size
  1219. + 8 > mmio->line_size) {
  1220. dev_dbg(dev, "stat_offset crosses interleave boundary\n");
  1221. return -ENXIO;
  1222. }
  1223. return 0;
  1224. }
  1225. static void acpi_nfit_blk_region_disable(struct nvdimm_bus *nvdimm_bus,
  1226. struct device *dev)
  1227. {
  1228. struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
  1229. struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
  1230. struct nd_blk_region *ndbr = to_nd_blk_region(dev);
  1231. struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
  1232. int i;
  1233. if (!nfit_blk)
  1234. return; /* never enabled */
  1235. /* auto-free BLK spa mappings */
  1236. for (i = 0; i < 2; i++) {
  1237. struct nfit_blk_mmio *mmio = &nfit_blk->mmio[i];
  1238. if (mmio->addr.base)
  1239. nfit_spa_unmap(acpi_desc, mmio->spa);
  1240. }
  1241. nd_blk_region_set_provider_data(ndbr, NULL);
  1242. /* devm will free nfit_blk */
  1243. }
  1244. static int ars_get_cap(struct nvdimm_bus_descriptor *nd_desc,
  1245. struct nd_cmd_ars_cap *cmd, u64 addr, u64 length)
  1246. {
  1247. cmd->address = addr;
  1248. cmd->length = length;
  1249. return nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
  1250. sizeof(*cmd));
  1251. }
  1252. static int ars_do_start(struct nvdimm_bus_descriptor *nd_desc,
  1253. struct nd_cmd_ars_start *cmd, u64 addr, u64 length)
  1254. {
  1255. int rc;
  1256. cmd->address = addr;
  1257. cmd->length = length;
  1258. cmd->type = ND_ARS_PERSISTENT;
  1259. while (1) {
  1260. rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, cmd,
  1261. sizeof(*cmd));
  1262. if (rc)
  1263. return rc;
  1264. switch (cmd->status) {
  1265. case 0:
  1266. return 0;
  1267. case 1:
  1268. /* ARS unsupported, but we should never get here */
  1269. return 0;
  1270. case 2:
  1271. return -EINVAL;
  1272. case 3:
  1273. /* ARS is in progress */
  1274. msleep(1000);
  1275. break;
  1276. default:
  1277. return -ENXIO;
  1278. }
  1279. }
  1280. }
  1281. static int ars_get_status(struct nvdimm_bus_descriptor *nd_desc,
  1282. struct nd_cmd_ars_status *cmd)
  1283. {
  1284. int rc;
  1285. while (1) {
  1286. rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, cmd,
  1287. sizeof(*cmd));
  1288. if (rc || cmd->status & 0xffff)
  1289. return -ENXIO;
  1290. /* Check extended status (Upper two bytes) */
  1291. switch (cmd->status >> 16) {
  1292. case 0:
  1293. return 0;
  1294. case 1:
  1295. /* ARS is in progress */
  1296. msleep(1000);
  1297. break;
  1298. case 2:
  1299. /* No ARS performed for the current boot */
  1300. return 0;
  1301. default:
  1302. return -ENXIO;
  1303. }
  1304. }
  1305. }
  1306. static int ars_status_process_records(struct nvdimm_bus *nvdimm_bus,
  1307. struct nd_cmd_ars_status *ars_status, u64 start)
  1308. {
  1309. int rc;
  1310. u32 i;
  1311. /*
  1312. * The address field returned by ars_status should be either
  1313. * less than or equal to the address we last started ARS for.
  1314. * The (start, length) returned by ars_status should also have
  1315. * non-zero overlap with the range we started ARS for.
  1316. * If this is not the case, bail.
  1317. */
  1318. if (ars_status->address > start ||
  1319. (ars_status->address + ars_status->length < start))
  1320. return -ENXIO;
  1321. for (i = 0; i < ars_status->num_records; i++) {
  1322. rc = nvdimm_bus_add_poison(nvdimm_bus,
  1323. ars_status->records[i].err_address,
  1324. ars_status->records[i].length);
  1325. if (rc)
  1326. return rc;
  1327. }
  1328. return 0;
  1329. }
  1330. static int acpi_nfit_find_poison(struct acpi_nfit_desc *acpi_desc,
  1331. struct nd_region_desc *ndr_desc)
  1332. {
  1333. struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
  1334. struct nvdimm_bus *nvdimm_bus = acpi_desc->nvdimm_bus;
  1335. struct nd_cmd_ars_status *ars_status = NULL;
  1336. struct nd_cmd_ars_start *ars_start = NULL;
  1337. struct nd_cmd_ars_cap *ars_cap = NULL;
  1338. u64 start, len, cur, remaining;
  1339. int rc;
  1340. ars_cap = kzalloc(sizeof(*ars_cap), GFP_KERNEL);
  1341. if (!ars_cap)
  1342. return -ENOMEM;
  1343. start = ndr_desc->res->start;
  1344. len = ndr_desc->res->end - ndr_desc->res->start + 1;
  1345. rc = ars_get_cap(nd_desc, ars_cap, start, len);
  1346. if (rc)
  1347. goto out;
  1348. /*
  1349. * If ARS is unsupported, or if the 'Persistent Memory Scrub' flag in
  1350. * extended status is not set, skip this but continue initialization
  1351. */
  1352. if ((ars_cap->status & 0xffff) ||
  1353. !(ars_cap->status >> 16 & ND_ARS_PERSISTENT)) {
  1354. dev_warn(acpi_desc->dev,
  1355. "ARS unsupported (status: 0x%x), won't create an error list\n",
  1356. ars_cap->status);
  1357. goto out;
  1358. }
  1359. /*
  1360. * Check if a full-range ARS has been run. If so, use those results
  1361. * without having to start a new ARS.
  1362. */
  1363. ars_status = kzalloc(ars_cap->max_ars_out + sizeof(*ars_status),
  1364. GFP_KERNEL);
  1365. if (!ars_status) {
  1366. rc = -ENOMEM;
  1367. goto out;
  1368. }
  1369. rc = ars_get_status(nd_desc, ars_status);
  1370. if (rc)
  1371. goto out;
  1372. if (ars_status->address <= start &&
  1373. (ars_status->address + ars_status->length >= start + len)) {
  1374. rc = ars_status_process_records(nvdimm_bus, ars_status, start);
  1375. goto out;
  1376. }
  1377. /*
  1378. * ARS_STATUS can overflow if the number of poison entries found is
  1379. * greater than the maximum buffer size (ars_cap->max_ars_out)
  1380. * To detect overflow, check if the length field of ars_status
  1381. * is less than the length we supplied. If so, process the
  1382. * error entries we got, adjust the start point, and start again
  1383. */
  1384. ars_start = kzalloc(sizeof(*ars_start), GFP_KERNEL);
  1385. if (!ars_start)
  1386. return -ENOMEM;
  1387. cur = start;
  1388. remaining = len;
  1389. do {
  1390. u64 done, end;
  1391. rc = ars_do_start(nd_desc, ars_start, cur, remaining);
  1392. if (rc)
  1393. goto out;
  1394. rc = ars_get_status(nd_desc, ars_status);
  1395. if (rc)
  1396. goto out;
  1397. rc = ars_status_process_records(nvdimm_bus, ars_status, cur);
  1398. if (rc)
  1399. goto out;
  1400. end = min(cur + remaining,
  1401. ars_status->address + ars_status->length);
  1402. done = end - cur;
  1403. cur += done;
  1404. remaining -= done;
  1405. } while (remaining);
  1406. out:
  1407. kfree(ars_cap);
  1408. kfree(ars_start);
  1409. kfree(ars_status);
  1410. return rc;
  1411. }
  1412. static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
  1413. struct nd_mapping *nd_mapping, struct nd_region_desc *ndr_desc,
  1414. struct acpi_nfit_memory_map *memdev,
  1415. struct acpi_nfit_system_address *spa)
  1416. {
  1417. struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
  1418. memdev->device_handle);
  1419. struct nd_blk_region_desc *ndbr_desc;
  1420. struct nfit_mem *nfit_mem;
  1421. int blk_valid = 0;
  1422. if (!nvdimm) {
  1423. dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
  1424. spa->range_index, memdev->device_handle);
  1425. return -ENODEV;
  1426. }
  1427. nd_mapping->nvdimm = nvdimm;
  1428. switch (nfit_spa_type(spa)) {
  1429. case NFIT_SPA_PM:
  1430. case NFIT_SPA_VOLATILE:
  1431. nd_mapping->start = memdev->address;
  1432. nd_mapping->size = memdev->region_size;
  1433. break;
  1434. case NFIT_SPA_DCR:
  1435. nfit_mem = nvdimm_provider_data(nvdimm);
  1436. if (!nfit_mem || !nfit_mem->bdw) {
  1437. dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
  1438. spa->range_index, nvdimm_name(nvdimm));
  1439. } else {
  1440. nd_mapping->size = nfit_mem->bdw->capacity;
  1441. nd_mapping->start = nfit_mem->bdw->start_address;
  1442. ndr_desc->num_lanes = nfit_mem->bdw->windows;
  1443. blk_valid = 1;
  1444. }
  1445. ndr_desc->nd_mapping = nd_mapping;
  1446. ndr_desc->num_mappings = blk_valid;
  1447. ndbr_desc = to_blk_region_desc(ndr_desc);
  1448. ndbr_desc->enable = acpi_nfit_blk_region_enable;
  1449. ndbr_desc->disable = acpi_nfit_blk_region_disable;
  1450. ndbr_desc->do_io = acpi_desc->blk_do_io;
  1451. if (!nvdimm_blk_region_create(acpi_desc->nvdimm_bus, ndr_desc))
  1452. return -ENOMEM;
  1453. break;
  1454. }
  1455. return 0;
  1456. }
  1457. static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
  1458. struct nfit_spa *nfit_spa)
  1459. {
  1460. static struct nd_mapping nd_mappings[ND_MAX_MAPPINGS];
  1461. struct acpi_nfit_system_address *spa = nfit_spa->spa;
  1462. struct nd_blk_region_desc ndbr_desc;
  1463. struct nd_region_desc *ndr_desc;
  1464. struct nfit_memdev *nfit_memdev;
  1465. struct nvdimm_bus *nvdimm_bus;
  1466. struct resource res;
  1467. int count = 0, rc;
  1468. if (nfit_spa->is_registered)
  1469. return 0;
  1470. if (spa->range_index == 0) {
  1471. dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
  1472. __func__);
  1473. return 0;
  1474. }
  1475. memset(&res, 0, sizeof(res));
  1476. memset(&nd_mappings, 0, sizeof(nd_mappings));
  1477. memset(&ndbr_desc, 0, sizeof(ndbr_desc));
  1478. res.start = spa->address;
  1479. res.end = res.start + spa->length - 1;
  1480. ndr_desc = &ndbr_desc.ndr_desc;
  1481. ndr_desc->res = &res;
  1482. ndr_desc->provider_data = nfit_spa;
  1483. ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
  1484. if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
  1485. ndr_desc->numa_node = acpi_map_pxm_to_online_node(
  1486. spa->proximity_domain);
  1487. else
  1488. ndr_desc->numa_node = NUMA_NO_NODE;
  1489. list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
  1490. struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
  1491. struct nd_mapping *nd_mapping;
  1492. if (memdev->range_index != spa->range_index)
  1493. continue;
  1494. if (count >= ND_MAX_MAPPINGS) {
  1495. dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
  1496. spa->range_index, ND_MAX_MAPPINGS);
  1497. return -ENXIO;
  1498. }
  1499. nd_mapping = &nd_mappings[count++];
  1500. rc = acpi_nfit_init_mapping(acpi_desc, nd_mapping, ndr_desc,
  1501. memdev, spa);
  1502. if (rc)
  1503. return rc;
  1504. }
  1505. ndr_desc->nd_mapping = nd_mappings;
  1506. ndr_desc->num_mappings = count;
  1507. rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
  1508. if (rc)
  1509. return rc;
  1510. nvdimm_bus = acpi_desc->nvdimm_bus;
  1511. if (nfit_spa_type(spa) == NFIT_SPA_PM) {
  1512. rc = acpi_nfit_find_poison(acpi_desc, ndr_desc);
  1513. if (rc) {
  1514. dev_err(acpi_desc->dev,
  1515. "error while performing ARS to find poison: %d\n",
  1516. rc);
  1517. return rc;
  1518. }
  1519. if (!nvdimm_pmem_region_create(nvdimm_bus, ndr_desc))
  1520. return -ENOMEM;
  1521. } else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) {
  1522. if (!nvdimm_volatile_region_create(nvdimm_bus, ndr_desc))
  1523. return -ENOMEM;
  1524. }
  1525. nfit_spa->is_registered = 1;
  1526. return 0;
  1527. }
  1528. static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
  1529. {
  1530. struct nfit_spa *nfit_spa;
  1531. list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
  1532. int rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
  1533. if (rc)
  1534. return rc;
  1535. }
  1536. return 0;
  1537. }
  1538. static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
  1539. struct nfit_table_prev *prev)
  1540. {
  1541. struct device *dev = acpi_desc->dev;
  1542. if (!list_empty(&prev->spas) ||
  1543. !list_empty(&prev->memdevs) ||
  1544. !list_empty(&prev->dcrs) ||
  1545. !list_empty(&prev->bdws) ||
  1546. !list_empty(&prev->idts) ||
  1547. !list_empty(&prev->flushes)) {
  1548. dev_err(dev, "new nfit deletes entries (unsupported)\n");
  1549. return -ENXIO;
  1550. }
  1551. return 0;
  1552. }
  1553. int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, acpi_size sz)
  1554. {
  1555. struct device *dev = acpi_desc->dev;
  1556. struct nfit_table_prev prev;
  1557. const void *end;
  1558. u8 *data;
  1559. int rc;
  1560. mutex_lock(&acpi_desc->init_mutex);
  1561. INIT_LIST_HEAD(&prev.spas);
  1562. INIT_LIST_HEAD(&prev.memdevs);
  1563. INIT_LIST_HEAD(&prev.dcrs);
  1564. INIT_LIST_HEAD(&prev.bdws);
  1565. INIT_LIST_HEAD(&prev.idts);
  1566. INIT_LIST_HEAD(&prev.flushes);
  1567. list_cut_position(&prev.spas, &acpi_desc->spas,
  1568. acpi_desc->spas.prev);
  1569. list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
  1570. acpi_desc->memdevs.prev);
  1571. list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
  1572. acpi_desc->dcrs.prev);
  1573. list_cut_position(&prev.bdws, &acpi_desc->bdws,
  1574. acpi_desc->bdws.prev);
  1575. list_cut_position(&prev.idts, &acpi_desc->idts,
  1576. acpi_desc->idts.prev);
  1577. list_cut_position(&prev.flushes, &acpi_desc->flushes,
  1578. acpi_desc->flushes.prev);
  1579. data = (u8 *) acpi_desc->nfit;
  1580. end = data + sz;
  1581. while (!IS_ERR_OR_NULL(data))
  1582. data = add_table(acpi_desc, &prev, data, end);
  1583. if (IS_ERR(data)) {
  1584. dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
  1585. PTR_ERR(data));
  1586. rc = PTR_ERR(data);
  1587. goto out_unlock;
  1588. }
  1589. rc = acpi_nfit_check_deletions(acpi_desc, &prev);
  1590. if (rc)
  1591. goto out_unlock;
  1592. if (nfit_mem_init(acpi_desc) != 0) {
  1593. rc = -ENOMEM;
  1594. goto out_unlock;
  1595. }
  1596. acpi_nfit_init_dsms(acpi_desc);
  1597. rc = acpi_nfit_register_dimms(acpi_desc);
  1598. if (rc)
  1599. goto out_unlock;
  1600. rc = acpi_nfit_register_regions(acpi_desc);
  1601. out_unlock:
  1602. mutex_unlock(&acpi_desc->init_mutex);
  1603. return rc;
  1604. }
  1605. EXPORT_SYMBOL_GPL(acpi_nfit_init);
  1606. static struct acpi_nfit_desc *acpi_nfit_desc_init(struct acpi_device *adev)
  1607. {
  1608. struct nvdimm_bus_descriptor *nd_desc;
  1609. struct acpi_nfit_desc *acpi_desc;
  1610. struct device *dev = &adev->dev;
  1611. acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
  1612. if (!acpi_desc)
  1613. return ERR_PTR(-ENOMEM);
  1614. dev_set_drvdata(dev, acpi_desc);
  1615. acpi_desc->dev = dev;
  1616. acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
  1617. nd_desc = &acpi_desc->nd_desc;
  1618. nd_desc->provider_name = "ACPI.NFIT";
  1619. nd_desc->ndctl = acpi_nfit_ctl;
  1620. nd_desc->attr_groups = acpi_nfit_attribute_groups;
  1621. acpi_desc->nvdimm_bus = nvdimm_bus_register(dev, nd_desc);
  1622. if (!acpi_desc->nvdimm_bus) {
  1623. devm_kfree(dev, acpi_desc);
  1624. return ERR_PTR(-ENXIO);
  1625. }
  1626. INIT_LIST_HEAD(&acpi_desc->spa_maps);
  1627. INIT_LIST_HEAD(&acpi_desc->spas);
  1628. INIT_LIST_HEAD(&acpi_desc->dcrs);
  1629. INIT_LIST_HEAD(&acpi_desc->bdws);
  1630. INIT_LIST_HEAD(&acpi_desc->idts);
  1631. INIT_LIST_HEAD(&acpi_desc->flushes);
  1632. INIT_LIST_HEAD(&acpi_desc->memdevs);
  1633. INIT_LIST_HEAD(&acpi_desc->dimms);
  1634. mutex_init(&acpi_desc->spa_map_mutex);
  1635. mutex_init(&acpi_desc->init_mutex);
  1636. return acpi_desc;
  1637. }
  1638. static int acpi_nfit_add(struct acpi_device *adev)
  1639. {
  1640. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
  1641. struct acpi_nfit_desc *acpi_desc;
  1642. struct device *dev = &adev->dev;
  1643. struct acpi_table_header *tbl;
  1644. acpi_status status = AE_OK;
  1645. acpi_size sz;
  1646. int rc;
  1647. status = acpi_get_table_with_size("NFIT", 0, &tbl, &sz);
  1648. if (ACPI_FAILURE(status)) {
  1649. /* This is ok, we could have an nvdimm hotplugged later */
  1650. dev_dbg(dev, "failed to find NFIT at startup\n");
  1651. return 0;
  1652. }
  1653. acpi_desc = acpi_nfit_desc_init(adev);
  1654. if (IS_ERR(acpi_desc)) {
  1655. dev_err(dev, "%s: error initializing acpi_desc: %ld\n",
  1656. __func__, PTR_ERR(acpi_desc));
  1657. return PTR_ERR(acpi_desc);
  1658. }
  1659. /*
  1660. * Save the acpi header for later and then skip it,
  1661. * making nfit point to the first nfit table header.
  1662. */
  1663. acpi_desc->acpi_header = *tbl;
  1664. acpi_desc->nfit = (void *) tbl + sizeof(struct acpi_table_nfit);
  1665. sz -= sizeof(struct acpi_table_nfit);
  1666. /* Evaluate _FIT and override with that if present */
  1667. status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
  1668. if (ACPI_SUCCESS(status) && buf.length > 0) {
  1669. union acpi_object *obj;
  1670. /*
  1671. * Adjust for the acpi_object header of the _FIT
  1672. */
  1673. obj = buf.pointer;
  1674. if (obj->type == ACPI_TYPE_BUFFER) {
  1675. acpi_desc->nfit =
  1676. (struct acpi_nfit_header *)obj->buffer.pointer;
  1677. sz = obj->buffer.length;
  1678. } else
  1679. dev_dbg(dev, "%s invalid type %d, ignoring _FIT\n",
  1680. __func__, (int) obj->type);
  1681. }
  1682. rc = acpi_nfit_init(acpi_desc, sz);
  1683. if (rc) {
  1684. nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
  1685. return rc;
  1686. }
  1687. return 0;
  1688. }
  1689. static int acpi_nfit_remove(struct acpi_device *adev)
  1690. {
  1691. struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(&adev->dev);
  1692. nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
  1693. return 0;
  1694. }
  1695. static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
  1696. {
  1697. struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(&adev->dev);
  1698. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
  1699. struct acpi_nfit_header *nfit_saved;
  1700. union acpi_object *obj;
  1701. struct device *dev = &adev->dev;
  1702. acpi_status status;
  1703. int ret;
  1704. dev_dbg(dev, "%s: event: %d\n", __func__, event);
  1705. device_lock(dev);
  1706. if (!dev->driver) {
  1707. /* dev->driver may be null if we're being removed */
  1708. dev_dbg(dev, "%s: no driver found for dev\n", __func__);
  1709. goto out_unlock;
  1710. }
  1711. if (!acpi_desc) {
  1712. acpi_desc = acpi_nfit_desc_init(adev);
  1713. if (IS_ERR(acpi_desc)) {
  1714. dev_err(dev, "%s: error initializing acpi_desc: %ld\n",
  1715. __func__, PTR_ERR(acpi_desc));
  1716. goto out_unlock;
  1717. }
  1718. }
  1719. /* Evaluate _FIT */
  1720. status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
  1721. if (ACPI_FAILURE(status)) {
  1722. dev_err(dev, "failed to evaluate _FIT\n");
  1723. goto out_unlock;
  1724. }
  1725. nfit_saved = acpi_desc->nfit;
  1726. obj = buf.pointer;
  1727. if (obj->type == ACPI_TYPE_BUFFER) {
  1728. acpi_desc->nfit =
  1729. (struct acpi_nfit_header *)obj->buffer.pointer;
  1730. ret = acpi_nfit_init(acpi_desc, obj->buffer.length);
  1731. if (ret) {
  1732. /* Merge failed, restore old nfit, and exit */
  1733. acpi_desc->nfit = nfit_saved;
  1734. dev_err(dev, "failed to merge updated NFIT\n");
  1735. }
  1736. } else {
  1737. /* Bad _FIT, restore old nfit */
  1738. dev_err(dev, "Invalid _FIT\n");
  1739. }
  1740. kfree(buf.pointer);
  1741. out_unlock:
  1742. device_unlock(dev);
  1743. }
  1744. static const struct acpi_device_id acpi_nfit_ids[] = {
  1745. { "ACPI0012", 0 },
  1746. { "", 0 },
  1747. };
  1748. MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
  1749. static struct acpi_driver acpi_nfit_driver = {
  1750. .name = KBUILD_MODNAME,
  1751. .ids = acpi_nfit_ids,
  1752. .ops = {
  1753. .add = acpi_nfit_add,
  1754. .remove = acpi_nfit_remove,
  1755. .notify = acpi_nfit_notify,
  1756. },
  1757. };
  1758. static __init int nfit_init(void)
  1759. {
  1760. BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
  1761. BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
  1762. BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
  1763. BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
  1764. BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
  1765. BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
  1766. BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
  1767. acpi_str_to_uuid(UUID_VOLATILE_MEMORY, nfit_uuid[NFIT_SPA_VOLATILE]);
  1768. acpi_str_to_uuid(UUID_PERSISTENT_MEMORY, nfit_uuid[NFIT_SPA_PM]);
  1769. acpi_str_to_uuid(UUID_CONTROL_REGION, nfit_uuid[NFIT_SPA_DCR]);
  1770. acpi_str_to_uuid(UUID_DATA_REGION, nfit_uuid[NFIT_SPA_BDW]);
  1771. acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_VDISK]);
  1772. acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_CD, nfit_uuid[NFIT_SPA_VCD]);
  1773. acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_PDISK]);
  1774. acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_CD, nfit_uuid[NFIT_SPA_PCD]);
  1775. acpi_str_to_uuid(UUID_NFIT_BUS, nfit_uuid[NFIT_DEV_BUS]);
  1776. acpi_str_to_uuid(UUID_NFIT_DIMM, nfit_uuid[NFIT_DEV_DIMM]);
  1777. return acpi_bus_register_driver(&acpi_nfit_driver);
  1778. }
  1779. static __exit void nfit_exit(void)
  1780. {
  1781. acpi_bus_unregister_driver(&acpi_nfit_driver);
  1782. }
  1783. module_init(nfit_init);
  1784. module_exit(nfit_exit);
  1785. MODULE_LICENSE("GPL v2");
  1786. MODULE_AUTHOR("Intel Corporation");