nfit.c 51 KB

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