vmbus_drv.c 39 KB

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