vmbus_drv.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  1. /*
  2. * Copyright (c) 2009, Microsoft Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  15. * Place - Suite 330, Boston, MA 02111-1307 USA.
  16. *
  17. * Authors:
  18. * Haiyang Zhang <haiyangz@microsoft.com>
  19. * Hank Janssen <hjanssen@microsoft.com>
  20. * K. Y. Srinivasan <kys@microsoft.com>
  21. *
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/device.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/sysctl.h>
  29. #include <linux/slab.h>
  30. #include <linux/acpi.h>
  31. #include <linux/completion.h>
  32. #include <linux/hyperv.h>
  33. #include <linux/kernel_stat.h>
  34. #include <linux/clockchips.h>
  35. #include <linux/cpu.h>
  36. #include <linux/sched/task_stack.h>
  37. #include <asm/hyperv.h>
  38. #include <asm/hypervisor.h>
  39. #include <asm/mshyperv.h>
  40. #include <linux/notifier.h>
  41. #include <linux/ptrace.h>
  42. #include <linux/screen_info.h>
  43. #include <linux/kdebug.h>
  44. #include <linux/efi.h>
  45. #include <linux/random.h>
  46. #include "hyperv_vmbus.h"
  47. struct vmbus_dynid {
  48. struct list_head node;
  49. struct hv_vmbus_device_id id;
  50. };
  51. static struct acpi_device *hv_acpi_dev;
  52. static struct completion probe_event;
  53. static int hyperv_cpuhp_online;
  54. static int hyperv_panic_event(struct notifier_block *nb, unsigned long val,
  55. void *args)
  56. {
  57. struct pt_regs *regs;
  58. regs = current_pt_regs();
  59. hyperv_report_panic(regs);
  60. return NOTIFY_DONE;
  61. }
  62. static int hyperv_die_event(struct notifier_block *nb, unsigned long val,
  63. void *args)
  64. {
  65. struct die_args *die = (struct die_args *)args;
  66. struct pt_regs *regs = die->regs;
  67. hyperv_report_panic(regs);
  68. return NOTIFY_DONE;
  69. }
  70. static struct notifier_block hyperv_die_block = {
  71. .notifier_call = hyperv_die_event,
  72. };
  73. static struct notifier_block hyperv_panic_block = {
  74. .notifier_call = hyperv_panic_event,
  75. };
  76. static const char *fb_mmio_name = "fb_range";
  77. static struct resource *fb_mmio;
  78. static struct resource *hyperv_mmio;
  79. static DEFINE_SEMAPHORE(hyperv_mmio_lock);
  80. static int vmbus_exists(void)
  81. {
  82. if (hv_acpi_dev == NULL)
  83. return -ENODEV;
  84. return 0;
  85. }
  86. #define VMBUS_ALIAS_LEN ((sizeof((struct hv_vmbus_device_id *)0)->guid) * 2)
  87. static void print_alias_name(struct hv_device *hv_dev, char *alias_name)
  88. {
  89. int i;
  90. for (i = 0; i < VMBUS_ALIAS_LEN; i += 2)
  91. sprintf(&alias_name[i], "%02x", hv_dev->dev_type.b[i/2]);
  92. }
  93. static u8 channel_monitor_group(struct vmbus_channel *channel)
  94. {
  95. return (u8)channel->offermsg.monitorid / 32;
  96. }
  97. static u8 channel_monitor_offset(struct vmbus_channel *channel)
  98. {
  99. return (u8)channel->offermsg.monitorid % 32;
  100. }
  101. static u32 channel_pending(struct vmbus_channel *channel,
  102. struct hv_monitor_page *monitor_page)
  103. {
  104. u8 monitor_group = channel_monitor_group(channel);
  105. return monitor_page->trigger_group[monitor_group].pending;
  106. }
  107. static u32 channel_latency(struct vmbus_channel *channel,
  108. struct hv_monitor_page *monitor_page)
  109. {
  110. u8 monitor_group = channel_monitor_group(channel);
  111. u8 monitor_offset = channel_monitor_offset(channel);
  112. return monitor_page->latency[monitor_group][monitor_offset];
  113. }
  114. static u32 channel_conn_id(struct vmbus_channel *channel,
  115. struct hv_monitor_page *monitor_page)
  116. {
  117. u8 monitor_group = channel_monitor_group(channel);
  118. u8 monitor_offset = channel_monitor_offset(channel);
  119. return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
  120. }
  121. static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
  122. char *buf)
  123. {
  124. struct hv_device *hv_dev = device_to_hv_device(dev);
  125. if (!hv_dev->channel)
  126. return -ENODEV;
  127. return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
  128. }
  129. static DEVICE_ATTR_RO(id);
  130. static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
  131. char *buf)
  132. {
  133. struct hv_device *hv_dev = device_to_hv_device(dev);
  134. if (!hv_dev->channel)
  135. return -ENODEV;
  136. return sprintf(buf, "%d\n", hv_dev->channel->state);
  137. }
  138. static DEVICE_ATTR_RO(state);
  139. static ssize_t monitor_id_show(struct device *dev,
  140. struct device_attribute *dev_attr, char *buf)
  141. {
  142. struct hv_device *hv_dev = device_to_hv_device(dev);
  143. if (!hv_dev->channel)
  144. return -ENODEV;
  145. return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
  146. }
  147. static DEVICE_ATTR_RO(monitor_id);
  148. static ssize_t class_id_show(struct device *dev,
  149. struct device_attribute *dev_attr, char *buf)
  150. {
  151. struct hv_device *hv_dev = device_to_hv_device(dev);
  152. if (!hv_dev->channel)
  153. return -ENODEV;
  154. return sprintf(buf, "{%pUl}\n",
  155. hv_dev->channel->offermsg.offer.if_type.b);
  156. }
  157. static DEVICE_ATTR_RO(class_id);
  158. static ssize_t device_id_show(struct device *dev,
  159. struct device_attribute *dev_attr, char *buf)
  160. {
  161. struct hv_device *hv_dev = device_to_hv_device(dev);
  162. if (!hv_dev->channel)
  163. return -ENODEV;
  164. return sprintf(buf, "{%pUl}\n",
  165. hv_dev->channel->offermsg.offer.if_instance.b);
  166. }
  167. static DEVICE_ATTR_RO(device_id);
  168. static ssize_t modalias_show(struct device *dev,
  169. struct device_attribute *dev_attr, char *buf)
  170. {
  171. struct hv_device *hv_dev = device_to_hv_device(dev);
  172. char alias_name[VMBUS_ALIAS_LEN + 1];
  173. print_alias_name(hv_dev, alias_name);
  174. return sprintf(buf, "vmbus:%s\n", alias_name);
  175. }
  176. static DEVICE_ATTR_RO(modalias);
  177. static ssize_t server_monitor_pending_show(struct device *dev,
  178. struct device_attribute *dev_attr,
  179. char *buf)
  180. {
  181. struct hv_device *hv_dev = device_to_hv_device(dev);
  182. if (!hv_dev->channel)
  183. return -ENODEV;
  184. return sprintf(buf, "%d\n",
  185. channel_pending(hv_dev->channel,
  186. vmbus_connection.monitor_pages[1]));
  187. }
  188. static DEVICE_ATTR_RO(server_monitor_pending);
  189. static ssize_t client_monitor_pending_show(struct device *dev,
  190. struct device_attribute *dev_attr,
  191. char *buf)
  192. {
  193. struct hv_device *hv_dev = device_to_hv_device(dev);
  194. if (!hv_dev->channel)
  195. return -ENODEV;
  196. return sprintf(buf, "%d\n",
  197. channel_pending(hv_dev->channel,
  198. vmbus_connection.monitor_pages[1]));
  199. }
  200. static DEVICE_ATTR_RO(client_monitor_pending);
  201. static ssize_t server_monitor_latency_show(struct device *dev,
  202. struct device_attribute *dev_attr,
  203. char *buf)
  204. {
  205. struct hv_device *hv_dev = device_to_hv_device(dev);
  206. if (!hv_dev->channel)
  207. return -ENODEV;
  208. return sprintf(buf, "%d\n",
  209. channel_latency(hv_dev->channel,
  210. vmbus_connection.monitor_pages[0]));
  211. }
  212. static DEVICE_ATTR_RO(server_monitor_latency);
  213. static ssize_t client_monitor_latency_show(struct device *dev,
  214. struct device_attribute *dev_attr,
  215. char *buf)
  216. {
  217. struct hv_device *hv_dev = device_to_hv_device(dev);
  218. if (!hv_dev->channel)
  219. return -ENODEV;
  220. return sprintf(buf, "%d\n",
  221. channel_latency(hv_dev->channel,
  222. vmbus_connection.monitor_pages[1]));
  223. }
  224. static DEVICE_ATTR_RO(client_monitor_latency);
  225. static ssize_t server_monitor_conn_id_show(struct device *dev,
  226. struct device_attribute *dev_attr,
  227. char *buf)
  228. {
  229. struct hv_device *hv_dev = device_to_hv_device(dev);
  230. if (!hv_dev->channel)
  231. return -ENODEV;
  232. return sprintf(buf, "%d\n",
  233. channel_conn_id(hv_dev->channel,
  234. vmbus_connection.monitor_pages[0]));
  235. }
  236. static DEVICE_ATTR_RO(server_monitor_conn_id);
  237. static ssize_t client_monitor_conn_id_show(struct device *dev,
  238. struct device_attribute *dev_attr,
  239. char *buf)
  240. {
  241. struct hv_device *hv_dev = device_to_hv_device(dev);
  242. if (!hv_dev->channel)
  243. return -ENODEV;
  244. return sprintf(buf, "%d\n",
  245. channel_conn_id(hv_dev->channel,
  246. vmbus_connection.monitor_pages[1]));
  247. }
  248. static DEVICE_ATTR_RO(client_monitor_conn_id);
  249. static ssize_t out_intr_mask_show(struct device *dev,
  250. struct device_attribute *dev_attr, char *buf)
  251. {
  252. struct hv_device *hv_dev = device_to_hv_device(dev);
  253. struct hv_ring_buffer_debug_info outbound;
  254. int ret;
  255. if (!hv_dev->channel)
  256. return -ENODEV;
  257. ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
  258. &outbound);
  259. if (ret < 0)
  260. return ret;
  261. return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
  262. }
  263. static DEVICE_ATTR_RO(out_intr_mask);
  264. static ssize_t out_read_index_show(struct device *dev,
  265. struct device_attribute *dev_attr, char *buf)
  266. {
  267. struct hv_device *hv_dev = device_to_hv_device(dev);
  268. struct hv_ring_buffer_debug_info outbound;
  269. int ret;
  270. if (!hv_dev->channel)
  271. return -ENODEV;
  272. ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
  273. &outbound);
  274. if (ret < 0)
  275. return ret;
  276. return sprintf(buf, "%d\n", outbound.current_read_index);
  277. }
  278. static DEVICE_ATTR_RO(out_read_index);
  279. static ssize_t out_write_index_show(struct device *dev,
  280. struct device_attribute *dev_attr,
  281. char *buf)
  282. {
  283. struct hv_device *hv_dev = device_to_hv_device(dev);
  284. struct hv_ring_buffer_debug_info outbound;
  285. int ret;
  286. if (!hv_dev->channel)
  287. return -ENODEV;
  288. ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
  289. &outbound);
  290. if (ret < 0)
  291. return ret;
  292. return sprintf(buf, "%d\n", outbound.current_write_index);
  293. }
  294. static DEVICE_ATTR_RO(out_write_index);
  295. static ssize_t out_read_bytes_avail_show(struct device *dev,
  296. struct device_attribute *dev_attr,
  297. char *buf)
  298. {
  299. struct hv_device *hv_dev = device_to_hv_device(dev);
  300. struct hv_ring_buffer_debug_info outbound;
  301. int ret;
  302. if (!hv_dev->channel)
  303. return -ENODEV;
  304. ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
  305. &outbound);
  306. if (ret < 0)
  307. return ret;
  308. return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
  309. }
  310. static DEVICE_ATTR_RO(out_read_bytes_avail);
  311. static ssize_t out_write_bytes_avail_show(struct device *dev,
  312. struct device_attribute *dev_attr,
  313. char *buf)
  314. {
  315. struct hv_device *hv_dev = device_to_hv_device(dev);
  316. struct hv_ring_buffer_debug_info outbound;
  317. int ret;
  318. if (!hv_dev->channel)
  319. return -ENODEV;
  320. ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
  321. &outbound);
  322. if (ret < 0)
  323. return ret;
  324. return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
  325. }
  326. static DEVICE_ATTR_RO(out_write_bytes_avail);
  327. static ssize_t in_intr_mask_show(struct device *dev,
  328. struct device_attribute *dev_attr, char *buf)
  329. {
  330. struct hv_device *hv_dev = device_to_hv_device(dev);
  331. struct hv_ring_buffer_debug_info inbound;
  332. int ret;
  333. if (!hv_dev->channel)
  334. return -ENODEV;
  335. ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  336. if (ret < 0)
  337. return ret;
  338. return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
  339. }
  340. static DEVICE_ATTR_RO(in_intr_mask);
  341. static ssize_t in_read_index_show(struct device *dev,
  342. struct device_attribute *dev_attr, char *buf)
  343. {
  344. struct hv_device *hv_dev = device_to_hv_device(dev);
  345. struct hv_ring_buffer_debug_info inbound;
  346. int ret;
  347. if (!hv_dev->channel)
  348. return -ENODEV;
  349. ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  350. if (ret < 0)
  351. return ret;
  352. return sprintf(buf, "%d\n", inbound.current_read_index);
  353. }
  354. static DEVICE_ATTR_RO(in_read_index);
  355. static ssize_t in_write_index_show(struct device *dev,
  356. struct device_attribute *dev_attr, char *buf)
  357. {
  358. struct hv_device *hv_dev = device_to_hv_device(dev);
  359. struct hv_ring_buffer_debug_info inbound;
  360. int ret;
  361. if (!hv_dev->channel)
  362. return -ENODEV;
  363. ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  364. if (ret < 0)
  365. return ret;
  366. return sprintf(buf, "%d\n", inbound.current_write_index);
  367. }
  368. static DEVICE_ATTR_RO(in_write_index);
  369. static ssize_t in_read_bytes_avail_show(struct device *dev,
  370. struct device_attribute *dev_attr,
  371. char *buf)
  372. {
  373. struct hv_device *hv_dev = device_to_hv_device(dev);
  374. struct hv_ring_buffer_debug_info inbound;
  375. int ret;
  376. if (!hv_dev->channel)
  377. return -ENODEV;
  378. ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  379. if (ret < 0)
  380. return ret;
  381. return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
  382. }
  383. static DEVICE_ATTR_RO(in_read_bytes_avail);
  384. static ssize_t in_write_bytes_avail_show(struct device *dev,
  385. struct device_attribute *dev_attr,
  386. char *buf)
  387. {
  388. struct hv_device *hv_dev = device_to_hv_device(dev);
  389. struct hv_ring_buffer_debug_info inbound;
  390. int ret;
  391. if (!hv_dev->channel)
  392. return -ENODEV;
  393. ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  394. if (ret < 0)
  395. return ret;
  396. return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
  397. }
  398. static DEVICE_ATTR_RO(in_write_bytes_avail);
  399. static ssize_t channel_vp_mapping_show(struct device *dev,
  400. struct device_attribute *dev_attr,
  401. char *buf)
  402. {
  403. struct hv_device *hv_dev = device_to_hv_device(dev);
  404. struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
  405. unsigned long flags;
  406. int buf_size = PAGE_SIZE, n_written, tot_written;
  407. struct list_head *cur;
  408. if (!channel)
  409. return -ENODEV;
  410. tot_written = snprintf(buf, buf_size, "%u:%u\n",
  411. channel->offermsg.child_relid, channel->target_cpu);
  412. spin_lock_irqsave(&channel->lock, flags);
  413. list_for_each(cur, &channel->sc_list) {
  414. if (tot_written >= buf_size - 1)
  415. break;
  416. cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
  417. n_written = scnprintf(buf + tot_written,
  418. buf_size - tot_written,
  419. "%u:%u\n",
  420. cur_sc->offermsg.child_relid,
  421. cur_sc->target_cpu);
  422. tot_written += n_written;
  423. }
  424. spin_unlock_irqrestore(&channel->lock, flags);
  425. return tot_written;
  426. }
  427. static DEVICE_ATTR_RO(channel_vp_mapping);
  428. static ssize_t vendor_show(struct device *dev,
  429. struct device_attribute *dev_attr,
  430. char *buf)
  431. {
  432. struct hv_device *hv_dev = device_to_hv_device(dev);
  433. return sprintf(buf, "0x%x\n", hv_dev->vendor_id);
  434. }
  435. static DEVICE_ATTR_RO(vendor);
  436. static ssize_t device_show(struct device *dev,
  437. struct device_attribute *dev_attr,
  438. char *buf)
  439. {
  440. struct hv_device *hv_dev = device_to_hv_device(dev);
  441. return sprintf(buf, "0x%x\n", hv_dev->device_id);
  442. }
  443. static DEVICE_ATTR_RO(device);
  444. /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
  445. static struct attribute *vmbus_dev_attrs[] = {
  446. &dev_attr_id.attr,
  447. &dev_attr_state.attr,
  448. &dev_attr_monitor_id.attr,
  449. &dev_attr_class_id.attr,
  450. &dev_attr_device_id.attr,
  451. &dev_attr_modalias.attr,
  452. &dev_attr_server_monitor_pending.attr,
  453. &dev_attr_client_monitor_pending.attr,
  454. &dev_attr_server_monitor_latency.attr,
  455. &dev_attr_client_monitor_latency.attr,
  456. &dev_attr_server_monitor_conn_id.attr,
  457. &dev_attr_client_monitor_conn_id.attr,
  458. &dev_attr_out_intr_mask.attr,
  459. &dev_attr_out_read_index.attr,
  460. &dev_attr_out_write_index.attr,
  461. &dev_attr_out_read_bytes_avail.attr,
  462. &dev_attr_out_write_bytes_avail.attr,
  463. &dev_attr_in_intr_mask.attr,
  464. &dev_attr_in_read_index.attr,
  465. &dev_attr_in_write_index.attr,
  466. &dev_attr_in_read_bytes_avail.attr,
  467. &dev_attr_in_write_bytes_avail.attr,
  468. &dev_attr_channel_vp_mapping.attr,
  469. &dev_attr_vendor.attr,
  470. &dev_attr_device.attr,
  471. NULL,
  472. };
  473. ATTRIBUTE_GROUPS(vmbus_dev);
  474. /*
  475. * vmbus_uevent - add uevent for our device
  476. *
  477. * This routine is invoked when a device is added or removed on the vmbus to
  478. * generate a uevent to udev in the userspace. The udev will then look at its
  479. * rule and the uevent generated here to load the appropriate driver
  480. *
  481. * The alias string will be of the form vmbus:guid where guid is the string
  482. * representation of the device guid (each byte of the guid will be
  483. * represented with two hex characters.
  484. */
  485. static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
  486. {
  487. struct hv_device *dev = device_to_hv_device(device);
  488. int ret;
  489. char alias_name[VMBUS_ALIAS_LEN + 1];
  490. print_alias_name(dev, alias_name);
  491. ret = add_uevent_var(env, "MODALIAS=vmbus:%s", alias_name);
  492. return ret;
  493. }
  494. static const uuid_le null_guid;
  495. static inline bool is_null_guid(const uuid_le *guid)
  496. {
  497. if (uuid_le_cmp(*guid, null_guid))
  498. return false;
  499. return true;
  500. }
  501. /*
  502. * Return a matching hv_vmbus_device_id pointer.
  503. * If there is no match, return NULL.
  504. */
  505. static const struct hv_vmbus_device_id *hv_vmbus_get_id(struct hv_driver *drv,
  506. const uuid_le *guid)
  507. {
  508. const struct hv_vmbus_device_id *id = NULL;
  509. struct vmbus_dynid *dynid;
  510. /* Look at the dynamic ids first, before the static ones */
  511. spin_lock(&drv->dynids.lock);
  512. list_for_each_entry(dynid, &drv->dynids.list, node) {
  513. if (!uuid_le_cmp(dynid->id.guid, *guid)) {
  514. id = &dynid->id;
  515. break;
  516. }
  517. }
  518. spin_unlock(&drv->dynids.lock);
  519. if (id)
  520. return id;
  521. id = drv->id_table;
  522. if (id == NULL)
  523. return NULL; /* empty device table */
  524. for (; !is_null_guid(&id->guid); id++)
  525. if (!uuid_le_cmp(id->guid, *guid))
  526. return id;
  527. return NULL;
  528. }
  529. /* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */
  530. static int vmbus_add_dynid(struct hv_driver *drv, uuid_le *guid)
  531. {
  532. struct vmbus_dynid *dynid;
  533. dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
  534. if (!dynid)
  535. return -ENOMEM;
  536. dynid->id.guid = *guid;
  537. spin_lock(&drv->dynids.lock);
  538. list_add_tail(&dynid->node, &drv->dynids.list);
  539. spin_unlock(&drv->dynids.lock);
  540. return driver_attach(&drv->driver);
  541. }
  542. static void vmbus_free_dynids(struct hv_driver *drv)
  543. {
  544. struct vmbus_dynid *dynid, *n;
  545. spin_lock(&drv->dynids.lock);
  546. list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
  547. list_del(&dynid->node);
  548. kfree(dynid);
  549. }
  550. spin_unlock(&drv->dynids.lock);
  551. }
  552. /*
  553. * store_new_id - sysfs frontend to vmbus_add_dynid()
  554. *
  555. * Allow GUIDs to be added to an existing driver via sysfs.
  556. */
  557. static ssize_t new_id_store(struct device_driver *driver, const char *buf,
  558. size_t count)
  559. {
  560. struct hv_driver *drv = drv_to_hv_drv(driver);
  561. uuid_le guid;
  562. ssize_t retval;
  563. retval = uuid_le_to_bin(buf, &guid);
  564. if (retval)
  565. return retval;
  566. if (hv_vmbus_get_id(drv, &guid))
  567. return -EEXIST;
  568. retval = vmbus_add_dynid(drv, &guid);
  569. if (retval)
  570. return retval;
  571. return count;
  572. }
  573. static DRIVER_ATTR_WO(new_id);
  574. /*
  575. * store_remove_id - remove a PCI device ID from this driver
  576. *
  577. * Removes a dynamic pci device ID to this driver.
  578. */
  579. static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
  580. size_t count)
  581. {
  582. struct hv_driver *drv = drv_to_hv_drv(driver);
  583. struct vmbus_dynid *dynid, *n;
  584. uuid_le guid;
  585. ssize_t retval;
  586. retval = uuid_le_to_bin(buf, &guid);
  587. if (retval)
  588. return retval;
  589. retval = -ENODEV;
  590. spin_lock(&drv->dynids.lock);
  591. list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
  592. struct hv_vmbus_device_id *id = &dynid->id;
  593. if (!uuid_le_cmp(id->guid, guid)) {
  594. list_del(&dynid->node);
  595. kfree(dynid);
  596. retval = count;
  597. break;
  598. }
  599. }
  600. spin_unlock(&drv->dynids.lock);
  601. return retval;
  602. }
  603. static DRIVER_ATTR_WO(remove_id);
  604. static struct attribute *vmbus_drv_attrs[] = {
  605. &driver_attr_new_id.attr,
  606. &driver_attr_remove_id.attr,
  607. NULL,
  608. };
  609. ATTRIBUTE_GROUPS(vmbus_drv);
  610. /*
  611. * vmbus_match - Attempt to match the specified device to the specified driver
  612. */
  613. static int vmbus_match(struct device *device, struct device_driver *driver)
  614. {
  615. struct hv_driver *drv = drv_to_hv_drv(driver);
  616. struct hv_device *hv_dev = device_to_hv_device(device);
  617. /* The hv_sock driver handles all hv_sock offers. */
  618. if (is_hvsock_channel(hv_dev->channel))
  619. return drv->hvsock;
  620. if (hv_vmbus_get_id(drv, &hv_dev->dev_type))
  621. return 1;
  622. return 0;
  623. }
  624. /*
  625. * vmbus_probe - Add the new vmbus's child device
  626. */
  627. static int vmbus_probe(struct device *child_device)
  628. {
  629. int ret = 0;
  630. struct hv_driver *drv =
  631. drv_to_hv_drv(child_device->driver);
  632. struct hv_device *dev = device_to_hv_device(child_device);
  633. const struct hv_vmbus_device_id *dev_id;
  634. dev_id = hv_vmbus_get_id(drv, &dev->dev_type);
  635. if (drv->probe) {
  636. ret = drv->probe(dev, dev_id);
  637. if (ret != 0)
  638. pr_err("probe failed for device %s (%d)\n",
  639. dev_name(child_device), ret);
  640. } else {
  641. pr_err("probe not set for driver %s\n",
  642. dev_name(child_device));
  643. ret = -ENODEV;
  644. }
  645. return ret;
  646. }
  647. /*
  648. * vmbus_remove - Remove a vmbus device
  649. */
  650. static int vmbus_remove(struct device *child_device)
  651. {
  652. struct hv_driver *drv;
  653. struct hv_device *dev = device_to_hv_device(child_device);
  654. if (child_device->driver) {
  655. drv = drv_to_hv_drv(child_device->driver);
  656. if (drv->remove)
  657. drv->remove(dev);
  658. }
  659. return 0;
  660. }
  661. /*
  662. * vmbus_shutdown - Shutdown a vmbus device
  663. */
  664. static void vmbus_shutdown(struct device *child_device)
  665. {
  666. struct hv_driver *drv;
  667. struct hv_device *dev = device_to_hv_device(child_device);
  668. /* The device may not be attached yet */
  669. if (!child_device->driver)
  670. return;
  671. drv = drv_to_hv_drv(child_device->driver);
  672. if (drv->shutdown)
  673. drv->shutdown(dev);
  674. }
  675. /*
  676. * vmbus_device_release - Final callback release of the vmbus child device
  677. */
  678. static void vmbus_device_release(struct device *device)
  679. {
  680. struct hv_device *hv_dev = device_to_hv_device(device);
  681. struct vmbus_channel *channel = hv_dev->channel;
  682. mutex_lock(&vmbus_connection.channel_mutex);
  683. hv_process_channel_removal(channel->offermsg.child_relid);
  684. mutex_unlock(&vmbus_connection.channel_mutex);
  685. kfree(hv_dev);
  686. }
  687. /* The one and only one */
  688. static struct bus_type hv_bus = {
  689. .name = "vmbus",
  690. .match = vmbus_match,
  691. .shutdown = vmbus_shutdown,
  692. .remove = vmbus_remove,
  693. .probe = vmbus_probe,
  694. .uevent = vmbus_uevent,
  695. .dev_groups = vmbus_dev_groups,
  696. .drv_groups = vmbus_drv_groups,
  697. };
  698. struct onmessage_work_context {
  699. struct work_struct work;
  700. struct hv_message msg;
  701. };
  702. static void vmbus_onmessage_work(struct work_struct *work)
  703. {
  704. struct onmessage_work_context *ctx;
  705. /* Do not process messages if we're in DISCONNECTED state */
  706. if (vmbus_connection.conn_state == DISCONNECTED)
  707. return;
  708. ctx = container_of(work, struct onmessage_work_context,
  709. work);
  710. vmbus_onmessage(&ctx->msg);
  711. kfree(ctx);
  712. }
  713. static void hv_process_timer_expiration(struct hv_message *msg,
  714. struct hv_per_cpu_context *hv_cpu)
  715. {
  716. struct clock_event_device *dev = hv_cpu->clk_evt;
  717. if (dev->event_handler)
  718. dev->event_handler(dev);
  719. vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
  720. }
  721. void vmbus_on_msg_dpc(unsigned long data)
  722. {
  723. struct hv_per_cpu_context *hv_cpu = (void *)data;
  724. void *page_addr = hv_cpu->synic_message_page;
  725. struct hv_message *msg = (struct hv_message *)page_addr +
  726. VMBUS_MESSAGE_SINT;
  727. struct vmbus_channel_message_header *hdr;
  728. const struct vmbus_channel_message_table_entry *entry;
  729. struct onmessage_work_context *ctx;
  730. u32 message_type = msg->header.message_type;
  731. if (message_type == HVMSG_NONE)
  732. /* no msg */
  733. return;
  734. hdr = (struct vmbus_channel_message_header *)msg->u.payload;
  735. if (hdr->msgtype >= CHANNELMSG_COUNT) {
  736. WARN_ONCE(1, "unknown msgtype=%d\n", hdr->msgtype);
  737. goto msg_handled;
  738. }
  739. entry = &channel_message_table[hdr->msgtype];
  740. if (!entry->message_handler)
  741. goto msg_handled;
  742. if (entry->handler_type == VMHT_BLOCKING) {
  743. ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
  744. if (ctx == NULL)
  745. return;
  746. INIT_WORK(&ctx->work, vmbus_onmessage_work);
  747. memcpy(&ctx->msg, msg, sizeof(*msg));
  748. /*
  749. * The host can generate a rescind message while we
  750. * may still be handling the original offer. We deal with
  751. * this condition by ensuring the processing is done on the
  752. * same CPU.
  753. */
  754. switch (hdr->msgtype) {
  755. case CHANNELMSG_RESCIND_CHANNELOFFER:
  756. /*
  757. * If we are handling the rescind message;
  758. * schedule the work on the global work queue.
  759. */
  760. schedule_work_on(vmbus_connection.connect_cpu,
  761. &ctx->work);
  762. break;
  763. case CHANNELMSG_OFFERCHANNEL:
  764. atomic_inc(&vmbus_connection.offer_in_progress);
  765. queue_work_on(vmbus_connection.connect_cpu,
  766. vmbus_connection.work_queue,
  767. &ctx->work);
  768. break;
  769. default:
  770. queue_work(vmbus_connection.work_queue, &ctx->work);
  771. }
  772. } else
  773. entry->message_handler(hdr);
  774. msg_handled:
  775. vmbus_signal_eom(msg, message_type);
  776. }
  777. /*
  778. * Direct callback for channels using other deferred processing
  779. */
  780. static void vmbus_channel_isr(struct vmbus_channel *channel)
  781. {
  782. void (*callback_fn)(void *);
  783. callback_fn = READ_ONCE(channel->onchannel_callback);
  784. if (likely(callback_fn != NULL))
  785. (*callback_fn)(channel->channel_callback_context);
  786. }
  787. /*
  788. * Schedule all channels with events pending
  789. */
  790. static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)
  791. {
  792. unsigned long *recv_int_page;
  793. u32 maxbits, relid;
  794. if (vmbus_proto_version < VERSION_WIN8) {
  795. maxbits = MAX_NUM_CHANNELS_SUPPORTED;
  796. recv_int_page = vmbus_connection.recv_int_page;
  797. } else {
  798. /*
  799. * When the host is win8 and beyond, the event page
  800. * can be directly checked to get the id of the channel
  801. * that has the interrupt pending.
  802. */
  803. void *page_addr = hv_cpu->synic_event_page;
  804. union hv_synic_event_flags *event
  805. = (union hv_synic_event_flags *)page_addr +
  806. VMBUS_MESSAGE_SINT;
  807. maxbits = HV_EVENT_FLAGS_COUNT;
  808. recv_int_page = event->flags;
  809. }
  810. if (unlikely(!recv_int_page))
  811. return;
  812. for_each_set_bit(relid, recv_int_page, maxbits) {
  813. struct vmbus_channel *channel;
  814. if (!sync_test_and_clear_bit(relid, recv_int_page))
  815. continue;
  816. /* Special case - vmbus channel protocol msg */
  817. if (relid == 0)
  818. continue;
  819. rcu_read_lock();
  820. /* Find channel based on relid */
  821. list_for_each_entry_rcu(channel, &hv_cpu->chan_list, percpu_list) {
  822. if (channel->offermsg.child_relid != relid)
  823. continue;
  824. if (channel->rescind)
  825. continue;
  826. switch (channel->callback_mode) {
  827. case HV_CALL_ISR:
  828. vmbus_channel_isr(channel);
  829. break;
  830. case HV_CALL_BATCHED:
  831. hv_begin_read(&channel->inbound);
  832. /* fallthrough */
  833. case HV_CALL_DIRECT:
  834. tasklet_schedule(&channel->callback_event);
  835. }
  836. }
  837. rcu_read_unlock();
  838. }
  839. }
  840. static void vmbus_isr(void)
  841. {
  842. struct hv_per_cpu_context *hv_cpu
  843. = this_cpu_ptr(hv_context.cpu_context);
  844. void *page_addr = hv_cpu->synic_event_page;
  845. struct hv_message *msg;
  846. union hv_synic_event_flags *event;
  847. bool handled = false;
  848. if (unlikely(page_addr == NULL))
  849. return;
  850. event = (union hv_synic_event_flags *)page_addr +
  851. VMBUS_MESSAGE_SINT;
  852. /*
  853. * Check for events before checking for messages. This is the order
  854. * in which events and messages are checked in Windows guests on
  855. * Hyper-V, and the Windows team suggested we do the same.
  856. */
  857. if ((vmbus_proto_version == VERSION_WS2008) ||
  858. (vmbus_proto_version == VERSION_WIN7)) {
  859. /* Since we are a child, we only need to check bit 0 */
  860. if (sync_test_and_clear_bit(0, event->flags))
  861. handled = true;
  862. } else {
  863. /*
  864. * Our host is win8 or above. The signaling mechanism
  865. * has changed and we can directly look at the event page.
  866. * If bit n is set then we have an interrup on the channel
  867. * whose id is n.
  868. */
  869. handled = true;
  870. }
  871. if (handled)
  872. vmbus_chan_sched(hv_cpu);
  873. page_addr = hv_cpu->synic_message_page;
  874. msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
  875. /* Check if there are actual msgs to be processed */
  876. if (msg->header.message_type != HVMSG_NONE) {
  877. if (msg->header.message_type == HVMSG_TIMER_EXPIRED)
  878. hv_process_timer_expiration(msg, hv_cpu);
  879. else
  880. tasklet_schedule(&hv_cpu->msg_dpc);
  881. }
  882. add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0);
  883. }
  884. /*
  885. * vmbus_bus_init -Main vmbus driver initialization routine.
  886. *
  887. * Here, we
  888. * - initialize the vmbus driver context
  889. * - invoke the vmbus hv main init routine
  890. * - retrieve the channel offers
  891. */
  892. static int vmbus_bus_init(void)
  893. {
  894. int ret;
  895. /* Hypervisor initialization...setup hypercall page..etc */
  896. ret = hv_init();
  897. if (ret != 0) {
  898. pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
  899. return ret;
  900. }
  901. ret = bus_register(&hv_bus);
  902. if (ret)
  903. return ret;
  904. hv_setup_vmbus_irq(vmbus_isr);
  905. ret = hv_synic_alloc();
  906. if (ret)
  907. goto err_alloc;
  908. /*
  909. * Initialize the per-cpu interrupt state and
  910. * connect to the host.
  911. */
  912. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv:online",
  913. hv_synic_init, hv_synic_cleanup);
  914. if (ret < 0)
  915. goto err_alloc;
  916. hyperv_cpuhp_online = ret;
  917. ret = vmbus_connect();
  918. if (ret)
  919. goto err_connect;
  920. /*
  921. * Only register if the crash MSRs are available
  922. */
  923. if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
  924. register_die_notifier(&hyperv_die_block);
  925. atomic_notifier_chain_register(&panic_notifier_list,
  926. &hyperv_panic_block);
  927. }
  928. vmbus_request_offers();
  929. return 0;
  930. err_connect:
  931. cpuhp_remove_state(hyperv_cpuhp_online);
  932. err_alloc:
  933. hv_synic_free();
  934. hv_remove_vmbus_irq();
  935. bus_unregister(&hv_bus);
  936. return ret;
  937. }
  938. /**
  939. * __vmbus_child_driver_register() - Register a vmbus's driver
  940. * @hv_driver: Pointer to driver structure you want to register
  941. * @owner: owner module of the drv
  942. * @mod_name: module name string
  943. *
  944. * Registers the given driver with Linux through the 'driver_register()' call
  945. * and sets up the hyper-v vmbus handling for this driver.
  946. * It will return the state of the 'driver_register()' call.
  947. *
  948. */
  949. int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
  950. {
  951. int ret;
  952. pr_info("registering driver %s\n", hv_driver->name);
  953. ret = vmbus_exists();
  954. if (ret < 0)
  955. return ret;
  956. hv_driver->driver.name = hv_driver->name;
  957. hv_driver->driver.owner = owner;
  958. hv_driver->driver.mod_name = mod_name;
  959. hv_driver->driver.bus = &hv_bus;
  960. spin_lock_init(&hv_driver->dynids.lock);
  961. INIT_LIST_HEAD(&hv_driver->dynids.list);
  962. ret = driver_register(&hv_driver->driver);
  963. return ret;
  964. }
  965. EXPORT_SYMBOL_GPL(__vmbus_driver_register);
  966. /**
  967. * vmbus_driver_unregister() - Unregister a vmbus's driver
  968. * @hv_driver: Pointer to driver structure you want to
  969. * un-register
  970. *
  971. * Un-register the given driver that was previous registered with a call to
  972. * vmbus_driver_register()
  973. */
  974. void vmbus_driver_unregister(struct hv_driver *hv_driver)
  975. {
  976. pr_info("unregistering driver %s\n", hv_driver->name);
  977. if (!vmbus_exists()) {
  978. driver_unregister(&hv_driver->driver);
  979. vmbus_free_dynids(hv_driver);
  980. }
  981. }
  982. EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
  983. /*
  984. * vmbus_device_create - Creates and registers a new child device
  985. * on the vmbus.
  986. */
  987. struct hv_device *vmbus_device_create(const uuid_le *type,
  988. const uuid_le *instance,
  989. struct vmbus_channel *channel)
  990. {
  991. struct hv_device *child_device_obj;
  992. child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
  993. if (!child_device_obj) {
  994. pr_err("Unable to allocate device object for child device\n");
  995. return NULL;
  996. }
  997. child_device_obj->channel = channel;
  998. memcpy(&child_device_obj->dev_type, type, sizeof(uuid_le));
  999. memcpy(&child_device_obj->dev_instance, instance,
  1000. sizeof(uuid_le));
  1001. child_device_obj->vendor_id = 0x1414; /* MSFT vendor ID */
  1002. return child_device_obj;
  1003. }
  1004. /*
  1005. * vmbus_device_register - Register the child device
  1006. */
  1007. int vmbus_device_register(struct hv_device *child_device_obj)
  1008. {
  1009. int ret = 0;
  1010. dev_set_name(&child_device_obj->device, "%pUl",
  1011. child_device_obj->channel->offermsg.offer.if_instance.b);
  1012. child_device_obj->device.bus = &hv_bus;
  1013. child_device_obj->device.parent = &hv_acpi_dev->dev;
  1014. child_device_obj->device.release = vmbus_device_release;
  1015. /*
  1016. * Register with the LDM. This will kick off the driver/device
  1017. * binding...which will eventually call vmbus_match() and vmbus_probe()
  1018. */
  1019. ret = device_register(&child_device_obj->device);
  1020. if (ret)
  1021. pr_err("Unable to register child device\n");
  1022. else
  1023. pr_debug("child device %s registered\n",
  1024. dev_name(&child_device_obj->device));
  1025. return ret;
  1026. }
  1027. /*
  1028. * vmbus_device_unregister - Remove the specified child device
  1029. * from the vmbus.
  1030. */
  1031. void vmbus_device_unregister(struct hv_device *device_obj)
  1032. {
  1033. pr_debug("child device %s unregistered\n",
  1034. dev_name(&device_obj->device));
  1035. /*
  1036. * Kick off the process of unregistering the device.
  1037. * This will call vmbus_remove() and eventually vmbus_device_release()
  1038. */
  1039. device_unregister(&device_obj->device);
  1040. }
  1041. /*
  1042. * VMBUS is an acpi enumerated device. Get the information we
  1043. * need from DSDT.
  1044. */
  1045. #define VTPM_BASE_ADDRESS 0xfed40000
  1046. static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
  1047. {
  1048. resource_size_t start = 0;
  1049. resource_size_t end = 0;
  1050. struct resource *new_res;
  1051. struct resource **old_res = &hyperv_mmio;
  1052. struct resource **prev_res = NULL;
  1053. switch (res->type) {
  1054. /*
  1055. * "Address" descriptors are for bus windows. Ignore
  1056. * "memory" descriptors, which are for registers on
  1057. * devices.
  1058. */
  1059. case ACPI_RESOURCE_TYPE_ADDRESS32:
  1060. start = res->data.address32.address.minimum;
  1061. end = res->data.address32.address.maximum;
  1062. break;
  1063. case ACPI_RESOURCE_TYPE_ADDRESS64:
  1064. start = res->data.address64.address.minimum;
  1065. end = res->data.address64.address.maximum;
  1066. break;
  1067. default:
  1068. /* Unused resource type */
  1069. return AE_OK;
  1070. }
  1071. /*
  1072. * Ignore ranges that are below 1MB, as they're not
  1073. * necessary or useful here.
  1074. */
  1075. if (end < 0x100000)
  1076. return AE_OK;
  1077. new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
  1078. if (!new_res)
  1079. return AE_NO_MEMORY;
  1080. /* If this range overlaps the virtual TPM, truncate it. */
  1081. if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
  1082. end = VTPM_BASE_ADDRESS;
  1083. new_res->name = "hyperv mmio";
  1084. new_res->flags = IORESOURCE_MEM;
  1085. new_res->start = start;
  1086. new_res->end = end;
  1087. /*
  1088. * If two ranges are adjacent, merge them.
  1089. */
  1090. do {
  1091. if (!*old_res) {
  1092. *old_res = new_res;
  1093. break;
  1094. }
  1095. if (((*old_res)->end + 1) == new_res->start) {
  1096. (*old_res)->end = new_res->end;
  1097. kfree(new_res);
  1098. break;
  1099. }
  1100. if ((*old_res)->start == new_res->end + 1) {
  1101. (*old_res)->start = new_res->start;
  1102. kfree(new_res);
  1103. break;
  1104. }
  1105. if ((*old_res)->start > new_res->end) {
  1106. new_res->sibling = *old_res;
  1107. if (prev_res)
  1108. (*prev_res)->sibling = new_res;
  1109. *old_res = new_res;
  1110. break;
  1111. }
  1112. prev_res = old_res;
  1113. old_res = &(*old_res)->sibling;
  1114. } while (1);
  1115. return AE_OK;
  1116. }
  1117. static int vmbus_acpi_remove(struct acpi_device *device)
  1118. {
  1119. struct resource *cur_res;
  1120. struct resource *next_res;
  1121. if (hyperv_mmio) {
  1122. if (fb_mmio) {
  1123. __release_region(hyperv_mmio, fb_mmio->start,
  1124. resource_size(fb_mmio));
  1125. fb_mmio = NULL;
  1126. }
  1127. for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
  1128. next_res = cur_res->sibling;
  1129. kfree(cur_res);
  1130. }
  1131. }
  1132. return 0;
  1133. }
  1134. static void vmbus_reserve_fb(void)
  1135. {
  1136. int size;
  1137. /*
  1138. * Make a claim for the frame buffer in the resource tree under the
  1139. * first node, which will be the one below 4GB. The length seems to
  1140. * be underreported, particularly in a Generation 1 VM. So start out
  1141. * reserving a larger area and make it smaller until it succeeds.
  1142. */
  1143. if (screen_info.lfb_base) {
  1144. if (efi_enabled(EFI_BOOT))
  1145. size = max_t(__u32, screen_info.lfb_size, 0x800000);
  1146. else
  1147. size = max_t(__u32, screen_info.lfb_size, 0x4000000);
  1148. for (; !fb_mmio && (size >= 0x100000); size >>= 1) {
  1149. fb_mmio = __request_region(hyperv_mmio,
  1150. screen_info.lfb_base, size,
  1151. fb_mmio_name, 0);
  1152. }
  1153. }
  1154. }
  1155. /**
  1156. * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
  1157. * @new: If successful, supplied a pointer to the
  1158. * allocated MMIO space.
  1159. * @device_obj: Identifies the caller
  1160. * @min: Minimum guest physical address of the
  1161. * allocation
  1162. * @max: Maximum guest physical address
  1163. * @size: Size of the range to be allocated
  1164. * @align: Alignment of the range to be allocated
  1165. * @fb_overlap_ok: Whether this allocation can be allowed
  1166. * to overlap the video frame buffer.
  1167. *
  1168. * This function walks the resources granted to VMBus by the
  1169. * _CRS object in the ACPI namespace underneath the parent
  1170. * "bridge" whether that's a root PCI bus in the Generation 1
  1171. * case or a Module Device in the Generation 2 case. It then
  1172. * attempts to allocate from the global MMIO pool in a way that
  1173. * matches the constraints supplied in these parameters and by
  1174. * that _CRS.
  1175. *
  1176. * Return: 0 on success, -errno on failure
  1177. */
  1178. int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
  1179. resource_size_t min, resource_size_t max,
  1180. resource_size_t size, resource_size_t align,
  1181. bool fb_overlap_ok)
  1182. {
  1183. struct resource *iter, *shadow;
  1184. resource_size_t range_min, range_max, start;
  1185. const char *dev_n = dev_name(&device_obj->device);
  1186. int retval;
  1187. retval = -ENXIO;
  1188. down(&hyperv_mmio_lock);
  1189. /*
  1190. * If overlaps with frame buffers are allowed, then first attempt to
  1191. * make the allocation from within the reserved region. Because it
  1192. * is already reserved, no shadow allocation is necessary.
  1193. */
  1194. if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
  1195. !(max < fb_mmio->start)) {
  1196. range_min = fb_mmio->start;
  1197. range_max = fb_mmio->end;
  1198. start = (range_min + align - 1) & ~(align - 1);
  1199. for (; start + size - 1 <= range_max; start += align) {
  1200. *new = request_mem_region_exclusive(start, size, dev_n);
  1201. if (*new) {
  1202. retval = 0;
  1203. goto exit;
  1204. }
  1205. }
  1206. }
  1207. for (iter = hyperv_mmio; iter; iter = iter->sibling) {
  1208. if ((iter->start >= max) || (iter->end <= min))
  1209. continue;
  1210. range_min = iter->start;
  1211. range_max = iter->end;
  1212. start = (range_min + align - 1) & ~(align - 1);
  1213. for (; start + size - 1 <= range_max; start += align) {
  1214. shadow = __request_region(iter, start, size, NULL,
  1215. IORESOURCE_BUSY);
  1216. if (!shadow)
  1217. continue;
  1218. *new = request_mem_region_exclusive(start, size, dev_n);
  1219. if (*new) {
  1220. shadow->name = (char *)*new;
  1221. retval = 0;
  1222. goto exit;
  1223. }
  1224. __release_region(iter, start, size);
  1225. }
  1226. }
  1227. exit:
  1228. up(&hyperv_mmio_lock);
  1229. return retval;
  1230. }
  1231. EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
  1232. /**
  1233. * vmbus_free_mmio() - Free a memory-mapped I/O range.
  1234. * @start: Base address of region to release.
  1235. * @size: Size of the range to be allocated
  1236. *
  1237. * This function releases anything requested by
  1238. * vmbus_mmio_allocate().
  1239. */
  1240. void vmbus_free_mmio(resource_size_t start, resource_size_t size)
  1241. {
  1242. struct resource *iter;
  1243. down(&hyperv_mmio_lock);
  1244. for (iter = hyperv_mmio; iter; iter = iter->sibling) {
  1245. if ((iter->start >= start + size) || (iter->end <= start))
  1246. continue;
  1247. __release_region(iter, start, size);
  1248. }
  1249. release_mem_region(start, size);
  1250. up(&hyperv_mmio_lock);
  1251. }
  1252. EXPORT_SYMBOL_GPL(vmbus_free_mmio);
  1253. static int vmbus_acpi_add(struct acpi_device *device)
  1254. {
  1255. acpi_status result;
  1256. int ret_val = -ENODEV;
  1257. struct acpi_device *ancestor;
  1258. hv_acpi_dev = device;
  1259. result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  1260. vmbus_walk_resources, NULL);
  1261. if (ACPI_FAILURE(result))
  1262. goto acpi_walk_err;
  1263. /*
  1264. * Some ancestor of the vmbus acpi device (Gen1 or Gen2
  1265. * firmware) is the VMOD that has the mmio ranges. Get that.
  1266. */
  1267. for (ancestor = device->parent; ancestor; ancestor = ancestor->parent) {
  1268. result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
  1269. vmbus_walk_resources, NULL);
  1270. if (ACPI_FAILURE(result))
  1271. continue;
  1272. if (hyperv_mmio) {
  1273. vmbus_reserve_fb();
  1274. break;
  1275. }
  1276. }
  1277. ret_val = 0;
  1278. acpi_walk_err:
  1279. complete(&probe_event);
  1280. if (ret_val)
  1281. vmbus_acpi_remove(device);
  1282. return ret_val;
  1283. }
  1284. static const struct acpi_device_id vmbus_acpi_device_ids[] = {
  1285. {"VMBUS", 0},
  1286. {"VMBus", 0},
  1287. {"", 0},
  1288. };
  1289. MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
  1290. static struct acpi_driver vmbus_acpi_driver = {
  1291. .name = "vmbus",
  1292. .ids = vmbus_acpi_device_ids,
  1293. .ops = {
  1294. .add = vmbus_acpi_add,
  1295. .remove = vmbus_acpi_remove,
  1296. },
  1297. };
  1298. static void hv_kexec_handler(void)
  1299. {
  1300. hv_synic_clockevents_cleanup();
  1301. vmbus_initiate_unload(false);
  1302. vmbus_connection.conn_state = DISCONNECTED;
  1303. /* Make sure conn_state is set as hv_synic_cleanup checks for it */
  1304. mb();
  1305. cpuhp_remove_state(hyperv_cpuhp_online);
  1306. hyperv_cleanup();
  1307. };
  1308. static void hv_crash_handler(struct pt_regs *regs)
  1309. {
  1310. vmbus_initiate_unload(true);
  1311. /*
  1312. * In crash handler we can't schedule synic cleanup for all CPUs,
  1313. * doing the cleanup for current CPU only. This should be sufficient
  1314. * for kdump.
  1315. */
  1316. vmbus_connection.conn_state = DISCONNECTED;
  1317. hv_synic_cleanup(smp_processor_id());
  1318. hyperv_cleanup();
  1319. };
  1320. static int __init hv_acpi_init(void)
  1321. {
  1322. int ret, t;
  1323. if (x86_hyper_type != X86_HYPER_MS_HYPERV)
  1324. return -ENODEV;
  1325. init_completion(&probe_event);
  1326. /*
  1327. * Get ACPI resources first.
  1328. */
  1329. ret = acpi_bus_register_driver(&vmbus_acpi_driver);
  1330. if (ret)
  1331. return ret;
  1332. t = wait_for_completion_timeout(&probe_event, 5*HZ);
  1333. if (t == 0) {
  1334. ret = -ETIMEDOUT;
  1335. goto cleanup;
  1336. }
  1337. ret = vmbus_bus_init();
  1338. if (ret)
  1339. goto cleanup;
  1340. hv_setup_kexec_handler(hv_kexec_handler);
  1341. hv_setup_crash_handler(hv_crash_handler);
  1342. return 0;
  1343. cleanup:
  1344. acpi_bus_unregister_driver(&vmbus_acpi_driver);
  1345. hv_acpi_dev = NULL;
  1346. return ret;
  1347. }
  1348. static void __exit vmbus_exit(void)
  1349. {
  1350. int cpu;
  1351. hv_remove_kexec_handler();
  1352. hv_remove_crash_handler();
  1353. vmbus_connection.conn_state = DISCONNECTED;
  1354. hv_synic_clockevents_cleanup();
  1355. vmbus_disconnect();
  1356. hv_remove_vmbus_irq();
  1357. for_each_online_cpu(cpu) {
  1358. struct hv_per_cpu_context *hv_cpu
  1359. = per_cpu_ptr(hv_context.cpu_context, cpu);
  1360. tasklet_kill(&hv_cpu->msg_dpc);
  1361. }
  1362. vmbus_free_channels();
  1363. if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
  1364. unregister_die_notifier(&hyperv_die_block);
  1365. atomic_notifier_chain_unregister(&panic_notifier_list,
  1366. &hyperv_panic_block);
  1367. }
  1368. bus_unregister(&hv_bus);
  1369. cpuhp_remove_state(hyperv_cpuhp_online);
  1370. hv_synic_free();
  1371. acpi_bus_unregister_driver(&vmbus_acpi_driver);
  1372. }
  1373. MODULE_LICENSE("GPL");
  1374. subsys_initcall(hv_acpi_init);
  1375. module_exit(vmbus_exit);