Merge branch 'scripts'
[opensuse:kernel-source.git] / patches.xen / xen3-auto-common.diff
1 Subject: xen3 common
2 From: http://xenbits.xensource.com/linux-2.6.18-xen.hg (tip 1123:2d7a321286c9)
3 Patch-mainline: n/a
4 Acked-by: jbeulich@novell.com
5
6 List of files that don't require modification anymore (and hence got
7 removed from this patch), for reference and in case upstream wants to
8 take the forward porting patches:
9 2.6.19/include/linux/skbuff.h
10 2.6.19/net/core/dev.c
11 2.6.19/net/core/skbuff.c
12 2.6.19/net/ipv4/netfilter/nf_nat_proto_tcp.c
13 2.6.19/net/ipv4/netfilter/nf_nat_proto_udp.c
14 2.6.19/net/ipv4/xfrm4_output.c
15 2.6.22/include/linux/sched.h
16 2.6.22/kernel/softlockup.c
17 2.6.22/kernel/timer.c
18 2.6.25/mm/highmem.c
19 2.6.30/include/linux/pci_regs.h
20 2.6.35/kernel/time.c
21
22 --- head-2011-10-07.orig/drivers/Makefile       2011-10-07 10:46:17.000000000 +0200
23 +++ head-2011-10-07/drivers/Makefile    2011-06-30 15:36:40.000000000 +0200
24 @@ -45,6 +45,7 @@ obj-$(CONFIG_PARPORT)         += parport/
25  obj-y                          += base/ block/ misc/ mfd/ nfc/
26  obj-$(CONFIG_NUBUS)            += nubus/
27  obj-y                          += macintosh/
28 +obj-$(CONFIG_XEN)              += xen/
29  obj-$(CONFIG_IDE)              += ide/
30  obj-$(CONFIG_SCSI)             += scsi/
31  obj-$(CONFIG_ATA)              += ata/
32 --- head-2011-10-07.orig/drivers/acpi/Makefile  2011-10-07 10:46:17.000000000 +0200
33 +++ head-2011-10-07/drivers/acpi/Makefile       2011-04-13 11:25:29.000000000 +0200
34 @@ -67,6 +67,9 @@ obj-$(CONFIG_ACPI_CUSTOM_METHOD)+= custo
35  processor-y                    := processor_driver.o processor_throttling.o
36  processor-y                    += processor_idle.o processor_thermal.o
37  processor-$(CONFIG_CPU_FREQ)   += processor_perflib.o
38 +ifdef CONFIG_PROCESSOR_EXTERNAL_CONTROL
39 +processor-objs += processor_perflib.o processor_extcntl.o
40 +endif
41  
42  obj-$(CONFIG_ACPI_PROCESSOR_AGGREGATOR) += acpi_pad.o
43  obj-$(CONFIG_ACPI_IPMI)                += acpi_ipmi.o
44 --- head-2011-10-07.orig/drivers/acpi/acpica/hwsleep.c  2011-10-07 10:46:17.000000000 +0200
45 +++ head-2011-10-07/drivers/acpi/acpica/hwsleep.c       2011-04-13 11:25:29.000000000 +0200
46 @@ -236,7 +236,11 @@ acpi_status asmlinkage acpi_enter_sleep_
47         u32 pm1b_control;
48         struct acpi_bit_register_info *sleep_type_reg_info;
49         struct acpi_bit_register_info *sleep_enable_reg_info;
50 +#if !(defined(CONFIG_XEN) && defined(CONFIG_X86))
51         u32 in_value;
52 +#else
53 +       int err;
54 +#endif
55         struct acpi_object_list arg_list;
56         union acpi_object arg;
57         acpi_status status;
58 @@ -347,6 +351,7 @@ acpi_status asmlinkage acpi_enter_sleep_
59  
60         /* Write #2: Write both SLP_TYP + SLP_EN */
61  
62 +#if !(defined(CONFIG_XEN) && defined(CONFIG_X86))
63         status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control);
64         if (ACPI_FAILURE(status)) {
65                 return_ACPI_STATUS(status);
66 @@ -386,6 +391,16 @@ acpi_status asmlinkage acpi_enter_sleep_
67                 /* Spin until we wake */
68  
69         } while (!in_value);
70 +#else
71 +       /* PV ACPI just need check hypercall return value */
72 +       err = acpi_notify_hypervisor_state(sleep_state,
73 +                       PM1Acontrol, PM1Bcontrol);
74 +       if (err) {
75 +               ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
76 +                                 "Hypervisor failure [%d]\n", err));
77 +               return_ACPI_STATUS(AE_ERROR);
78 +       }
79 +#endif
80  
81         return_ACPI_STATUS(AE_OK);
82  }
83 --- head-2011-10-07.orig/drivers/acpi/processor_driver.c        2011-10-07 10:46:17.000000000 +0200
84 +++ head-2011-10-07/drivers/acpi/processor_driver.c     2011-04-13 11:25:29.000000000 +0200
85 @@ -325,7 +325,8 @@ static int acpi_processor_get_info(struc
86          */
87         if (pr->id == -1) {
88                 if (ACPI_FAILURE
89 -                   (acpi_processor_hotadd_init(pr->handle, &pr->id))) {
90 +                   (acpi_processor_hotadd_init(pr->handle, &pr->id)) &&
91 +                   !processor_cntl_external()) {
92                         return -ENODEV;
93                 }
94         }
95 @@ -376,7 +377,11 @@ static int acpi_processor_get_info(struc
96         return 0;
97  }
98  
99 +#ifndef CONFIG_XEN
100  static DEFINE_PER_CPU(void *, processor_device_array);
101 +#else
102 +static void *processor_device_array[NR_ACPI_CPUS];
103 +#endif
104  
105  static void acpi_processor_notify(struct acpi_device *device, u32 event)
106  {
107 @@ -462,8 +467,11 @@ static int __cpuinit acpi_processor_add(
108         strcpy(acpi_device_class(device), ACPI_PROCESSOR_CLASS);
109         device->driver_data = pr;
110  
111 +       processor_extcntl_init();
112 +
113         result = acpi_processor_get_info(device);
114 -       if (result) {
115 +       if (result ||
116 +           ((pr->id == -1) && !processor_cntl_external())) {
117                 /* Processor is physically not present */
118                 return 0;
119         }
120 @@ -473,23 +481,36 @@ static int __cpuinit acpi_processor_add(
121                 return 0;
122  #endif
123  
124 -       BUG_ON((pr->id >= nr_cpu_ids) || (pr->id < 0));
125 +       BUG_ON(!processor_cntl_external() &&
126 +              ((pr->id >= nr_cpu_ids) || (pr->id < 0)));
127  
128         /*
129          * Buggy BIOS check
130          * ACPI id of processors can be reported wrongly by the BIOS.
131          * Don't trust it blindly
132          */
133 +#ifndef CONFIG_XEN
134         if (per_cpu(processor_device_array, pr->id) != NULL &&
135             per_cpu(processor_device_array, pr->id) != device) {
136 +#else
137 +       BUG_ON(pr->acpi_id >= NR_ACPI_CPUS);
138 +       if (processor_device_array[pr->acpi_id] != NULL &&
139 +           processor_device_array[pr->acpi_id] != device) {
140 +#endif
141                 printk(KERN_WARNING "BIOS reported wrong ACPI id "
142                         "for the processor\n");
143                 result = -ENODEV;
144                 goto err_free_cpumask;
145         }
146 +#ifndef CONFIG_XEN
147         per_cpu(processor_device_array, pr->id) = device;
148  
149         per_cpu(processors, pr->id) = pr;
150 +#else
151 +       processor_device_array[pr->acpi_id] = device;
152 +       if (pr->id != -1)
153 +               per_cpu(processors, pr->id) = pr;
154 +#endif
155  
156         sysdev = get_cpu_sysdev(pr->id);
157         if (sysfs_create_link(&device->dev.kobj, &sysdev->kobj, "sysdev")) {
158 @@ -497,16 +518,28 @@ static int __cpuinit acpi_processor_add(
159                 goto err_free_cpumask;
160         }
161  
162 -#ifdef CONFIG_CPU_FREQ
163 +#if defined(CONFIG_CPU_FREQ) || defined(CONFIG_PROCESSOR_EXTERNAL_CONTROL)
164         acpi_processor_ppc_has_changed(pr, 0);
165  #endif
166 -       acpi_processor_get_throttling_info(pr);
167 -       acpi_processor_get_limit_info(pr);
168  
169 +       /*
170 +        * pr->id may equal to -1 while processor_cntl_external enabled.
171 +        * throttle and thermal module don't support this case.
172 +        * Tx only works when dom0 vcpu == pcpu num by far, as we give
173 +        * control to dom0.
174 +        */
175 +       if (pr->id != -1) {
176 +               acpi_processor_get_throttling_info(pr);
177 +               acpi_processor_get_limit_info(pr);
178 +       }
179  
180         if (cpuidle_get_driver() == &acpi_idle_driver)
181                 acpi_processor_power_init(pr, device);
182  
183 +       result = processor_extcntl_prepare(pr);
184 +       if (result)
185 +               goto end;
186 +
187         pr->cdev = thermal_cooling_device_register("Processor", device,
188                                                 &processor_cooling_ops);
189         if (IS_ERR(pr->cdev)) {
190 @@ -556,7 +589,7 @@ static int acpi_processor_remove(struct 
191  
192         pr = acpi_driver_data(device);
193  
194 -       if (pr->id >= nr_cpu_ids)
195 +       if (!processor_cntl_external() && pr->id >= nr_cpu_ids)
196                 goto free;
197  
198         if (type == ACPI_BUS_REMOVAL_EJECT) {
199 @@ -575,8 +608,14 @@ static int acpi_processor_remove(struct 
200                 pr->cdev = NULL;
201         }
202  
203 +#ifndef CONFIG_XEN
204         per_cpu(processors, pr->id) = NULL;
205         per_cpu(processor_device_array, pr->id) = NULL;
206 +#else
207 +       if (pr->id != -1)
208 +               per_cpu(processors, pr->id) = NULL;
209 +       processor_device_array[pr->acpi_id] = NULL;
210 +#endif
211  
212  free:
213         free_cpumask_var(pr->throttling.shared_cpu_map);
214 @@ -632,6 +671,10 @@ int acpi_processor_device_add(acpi_handl
215                 return -ENODEV;
216         }
217  
218 +       if (processor_cntl_external() && acpi_driver_data(*device))
219 +               processor_notify_external(acpi_driver_data(*device),
220 +                       PROCESSOR_HOTPLUG, HOTPLUG_TYPE_ADD);
221 +
222         return 0;
223  }
224  
225 @@ -661,6 +704,10 @@ static void acpi_processor_hotplug_notif
226                                             "Unable to add the device\n");
227                         break;
228                 }
229 +               pr = acpi_driver_data(device);
230 +               if (processor_cntl_external() && pr)
231 +                       processor_notify_external(pr,
232 +                                       PROCESSOR_HOTPLUG, HOTPLUG_TYPE_ADD);
233                 break;
234         case ACPI_NOTIFY_EJECT_REQUEST:
235                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
236 @@ -677,6 +724,9 @@ static void acpi_processor_hotplug_notif
237                                     "Driver data is NULL, dropping EJECT\n");
238                         return;
239                 }
240 +               if (processor_cntl_external())
241 +                       processor_notify_external(pr, PROCESSOR_HOTPLUG,
242 +                                               HOTPLUG_TYPE_REMOVE);
243                 break;
244         default:
245                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
246 @@ -741,6 +791,11 @@ static acpi_status acpi_processor_hotadd
247  
248  static int acpi_processor_handle_eject(struct acpi_processor *pr)
249  {
250 +#ifdef CONFIG_XEN
251 +       if (pr->id == -1)
252 +               return (0);
253 +#endif
254 +
255         if (cpu_online(pr->id))
256                 cpu_down(pr->id);
257  
258 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
259 +++ head-2011-10-07/drivers/acpi/processor_extcntl.c    2011-01-31 14:53:38.000000000 +0100
260 @@ -0,0 +1,241 @@
261 +/*
262 + * processor_extcntl.c - channel to external control logic
263 + *
264 + *  Copyright (C) 2008, Intel corporation
265 + *
266 + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
267 + *
268 + *  This program is free software; you can redistribute it and/or modify
269 + *  it under the terms of the GNU General Public License as published by
270 + *  the Free Software Foundation; either version 2 of the License, or (at
271 + *  your option) any later version.
272 + *
273 + *  This program is distributed in the hope that it will be useful, but
274 + *  WITHOUT ANY WARRANTY; without even the implied warranty of
275 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
276 + *  General Public License for more details.
277 + *
278 + *  You should have received a copy of the GNU General Public License along
279 + *  with this program; if not, write to the Free Software Foundation, Inc.,
280 + *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
281 + *
282 + */
283 +
284 +#include <linux/kernel.h>
285 +#include <linux/init.h>
286 +#include <linux/types.h>
287 +#include <linux/acpi.h>
288 +#include <linux/pm.h>
289 +#include <linux/cpu.h>
290 +
291 +#include <acpi/processor.h>
292 +
293 +#define ACPI_PROCESSOR_COMPONENT        0x01000000
294 +#define ACPI_PROCESSOR_CLASS            "processor"
295 +#define ACPI_PROCESSOR_DRIVER_NAME      "ACPI Processor Driver"
296 +#define _COMPONENT              ACPI_PROCESSOR_COMPONENT
297 +ACPI_MODULE_NAME("acpi_processor")
298 +
299 +static int processor_extcntl_parse_csd(struct acpi_processor *pr);
300 +static int processor_extcntl_get_performance(struct acpi_processor *pr);
301 +/*
302 + * External processor control logic may register with its own set of
303 + * ops to get ACPI related notification. One example is like VMM.
304 + */
305 +const struct processor_extcntl_ops *processor_extcntl_ops;
306 +EXPORT_SYMBOL(processor_extcntl_ops);
307 +
308 +static int processor_notify_smm(void)
309 +{
310 +       acpi_status status;
311 +       static int is_done = 0;
312 +
313 +       /* only need successfully notify BIOS once */
314 +       /* avoid double notification which may lead to unexpected result */
315 +       if (is_done)
316 +               return 0;
317 +
318 +       /* Can't write pstate_cnt to smi_cmd if either value is zero */
319 +       if ((!acpi_fadt.smi_cmd) || (!acpi_fadt.pstate_cnt)) {
320 +               ACPI_DEBUG_PRINT((ACPI_DB_INFO,"No SMI port or pstate_cnt\n"));
321 +               return 0;
322 +       }
323 +
324 +       ACPI_DEBUG_PRINT((ACPI_DB_INFO,
325 +               "Writing pstate_cnt [0x%x] to smi_cmd [0x%x]\n",
326 +               acpi_fadt.pstate_cnt, acpi_fadt.smi_cmd));
327 +
328 +       /* FADT v1 doesn't support pstate_cnt, many BIOS vendors use
329 +        * it anyway, so we need to support it... */
330 +       if (acpi_fadt_is_v1) {
331 +               ACPI_DEBUG_PRINT((ACPI_DB_INFO,
332 +                       "Using v1.0 FADT reserved value for pstate_cnt\n"));
333 +       }
334 +
335 +       status = acpi_os_write_port(acpi_fadt.smi_cmd,
336 +                                   (u32) acpi_fadt.pstate_cnt, 8);
337 +       if (ACPI_FAILURE(status))
338 +               return status;
339 +
340 +       is_done = 1;
341 +
342 +       return 0;
343 +}
344 +
345 +int processor_notify_external(struct acpi_processor *pr, int event, int type)
346 +{
347 +       int ret = -EINVAL;
348 +
349 +       if (!processor_cntl_external())
350 +               return -EINVAL;
351 +
352 +       switch (event) {
353 +       case PROCESSOR_PM_INIT:
354 +       case PROCESSOR_PM_CHANGE:
355 +               if ((type >= PM_TYPE_MAX) ||
356 +                       !processor_extcntl_ops->pm_ops[type])
357 +                       break;
358 +
359 +               ret = processor_extcntl_ops->pm_ops[type](pr, event);
360 +               break;
361 +       case PROCESSOR_HOTPLUG:
362 +               if (processor_extcntl_ops->hotplug)
363 +                       ret = processor_extcntl_ops->hotplug(pr, type);
364 +               break;
365 +       default:
366 +               printk(KERN_ERR "Unsupport processor events %d.\n", event);
367 +               break;
368 +       }
369 +
370 +       return ret;
371 +}
372 +
373 +/*
374 + * External control logic can decide to grab full or part of physical
375 + * processor control bits. Take a VMM for example, physical processors
376 + * are owned by VMM and thus existence information like hotplug is
377 + * always required to be notified to VMM. Similar is processor idle
378 + * state which is also necessarily controlled by VMM. But for other
379 + * control bits like performance/throttle states, VMM may choose to
380 + * control or not upon its own policy.
381 + */
382 +void processor_extcntl_init(void)
383 +{
384 +       if (!processor_extcntl_ops)
385 +               arch_acpi_processor_init_extcntl(&processor_extcntl_ops);
386 +}
387 +
388 +/*
389 + * This is called from ACPI processor init, and targeted to hold
390 + * some tricky housekeeping jobs to satisfy external control model.
391 + * For example, we may put dependency parse stub here for idle
392 + * and performance state. Those information may be not available
393 + * if splitting from dom0 control logic like cpufreq driver.
394 + */
395 +int processor_extcntl_prepare(struct acpi_processor *pr)
396 +{
397 +       /* parse cstate dependency information */
398 +       if (processor_pm_external())
399 +               processor_extcntl_parse_csd(pr);
400 +
401 +       /* Initialize performance states */
402 +       if (processor_pmperf_external())
403 +               processor_extcntl_get_performance(pr);
404 +
405 +       return 0;
406 +}
407 +
408 +/*
409 + * Currently no _CSD is implemented which is why existing ACPI code
410 + * doesn't parse _CSD at all. But to keep interface complete with
411 + * external control logic, we put a placeholder here for future
412 + * compatibility.
413 + */
414 +static int processor_extcntl_parse_csd(struct acpi_processor *pr)
415 +{
416 +       int i;
417 +
418 +       for (i = 0; i < pr->power.count; i++) {
419 +               if (!pr->power.states[i].valid)
420 +                       continue;
421 +
422 +               /* No dependency by default */
423 +               pr->power.states[i].domain_info = NULL;
424 +               pr->power.states[i].csd_count = 0;
425 +       }
426 +
427 +       return 0;
428 +}
429 +
430 +/*
431 + * Existing ACPI module does parse performance states at some point,
432 + * when acpi-cpufreq driver is loaded which however is something
433 + * we'd like to disable to avoid confliction with external control
434 + * logic. So we have to collect raw performance information here
435 + * when ACPI processor object is found and started.
436 + */
437 +static int processor_extcntl_get_performance(struct acpi_processor *pr)
438 +{
439 +       int ret;
440 +       struct acpi_processor_performance *perf;
441 +       struct acpi_psd_package *pdomain;
442 +
443 +       if (pr->performance)
444 +               return -EBUSY;
445 +
446 +       perf = kzalloc(sizeof(struct acpi_processor_performance), GFP_KERNEL);
447 +       if (!perf)
448 +               return -ENOMEM;
449 +
450 +       pr->performance = perf;
451 +       /* Get basic performance state information */
452 +       ret = acpi_processor_get_performance_info(pr);
453 +       if (ret < 0)
454 +               goto err_out;
455 +
456 +       /*
457 +        * Well, here we need retrieve performance dependency information
458 +        * from _PSD object. The reason why existing interface is not used
459 +        * is due to the reason that existing interface sticks to Linux cpu
460 +        * id to construct some bitmap, however we want to split ACPI
461 +        * processor objects from Linux cpu id logic. For example, even
462 +        * when Linux is configured as UP, we still want to parse all ACPI
463 +        * processor objects to external logic. In this case, it's preferred
464 +        * to use ACPI ID instead.
465 +        */
466 +       pdomain = &pr->performance->domain_info;
467 +       pdomain->num_processors = 0;
468 +       ret = acpi_processor_get_psd(pr);
469 +       if (ret < 0) {
470 +               /*
471 +                * _PSD is optional - assume no coordination if absent (or
472 +                * broken), matching native kernels' behavior.
473 +                */
474 +               pdomain->num_entries = ACPI_PSD_REV0_ENTRIES;
475 +               pdomain->revision = ACPI_PSD_REV0_REVISION;
476 +               pdomain->domain = pr->acpi_id;
477 +               pdomain->coord_type = DOMAIN_COORD_TYPE_SW_ALL;
478 +               pdomain->num_processors = 1;
479 +       }
480 +
481 +       /* Some sanity check */
482 +       if ((pdomain->revision != ACPI_PSD_REV0_REVISION) ||
483 +           (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) ||
484 +           ((pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL) &&
485 +            (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY) &&
486 +            (pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL))) {
487 +               ret = -EINVAL;
488 +               goto err_out;
489 +       }
490 +
491 +       /* Last step is to notify BIOS that external logic exists */
492 +       processor_notify_smm();
493 +
494 +       processor_notify_external(pr, PROCESSOR_PM_INIT, PM_TYPE_PERF);
495 +
496 +       return 0;
497 +err_out:
498 +       pr->performance = NULL;
499 +       kfree(perf);
500 +       return ret;
501 +}
502 --- head-2011-10-07.orig/drivers/acpi/processor_idle.c  2011-10-07 10:46:17.000000000 +0200
503 +++ head-2011-10-07/drivers/acpi/processor_idle.c       2011-09-07 14:39:37.000000000 +0200
504 @@ -458,7 +458,8 @@ static int acpi_processor_get_power_info
505                                  */
506                                 cx.entry_method = ACPI_CSTATE_HALT;
507                                 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
508 -                       } else {
509 +                       /* This doesn't apply to external control case */
510 +                       } else if (!processor_pm_external()) {
511                                 continue;
512                         }
513                         if (cx.type == ACPI_STATE_C1 &&
514 @@ -497,6 +498,12 @@ static int acpi_processor_get_power_info
515  
516                 cx.power = obj->integer.value;
517  
518 +#ifdef CONFIG_PROCESSOR_EXTERNAL_CONTROL
519 +               /* cache control methods to notify external logic */
520 +               if (processor_pm_external())
521 +                       memcpy(&cx.reg, reg, sizeof(*reg));
522 +#endif
523 +
524                 current_count++;
525                 memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
526  
527 @@ -518,7 +525,7 @@ static int acpi_processor_get_power_info
528                           current_count));
529  
530         /* Validate number of power states discovered */
531 -       if (current_count < 2)
532 +       if (current_count < (processor_pm_external() ? 1 : 2))
533                 status = -EFAULT;
534  
535        end:
536 @@ -1130,6 +1137,11 @@ int __cpuinit acpi_processor_power_init(
537                 if (cpuidle_register_device(&pr->power.dev))
538                         return -EIO;
539         }
540 +
541 +       if (processor_pm_external())
542 +               processor_notify_external(pr,
543 +                       PROCESSOR_PM_INIT, PM_TYPE_IDLE);
544 +
545         return 0;
546  }
547  
548 --- head-2011-10-07.orig/drivers/acpi/processor_perflib.c       2011-10-07 10:46:17.000000000 +0200
549 +++ head-2011-10-07/drivers/acpi/processor_perflib.c    2011-06-30 15:36:54.000000000 +0200
550 @@ -75,6 +75,7 @@ MODULE_PARM_DESC(ignore_ppc, "If the fre
551  
552  static int acpi_processor_ppc_status;
553  
554 +#ifdef CONFIG_CPU_FREQ
555  static int acpi_processor_ppc_notifier(struct notifier_block *nb,
556                                        unsigned long event, void *data)
557  {
558 @@ -117,6 +118,7 @@ static int acpi_processor_ppc_notifier(s
559  static struct notifier_block acpi_ppc_notifier_block = {
560         .notifier_call = acpi_processor_ppc_notifier,
561  };
562 +#endif /* CONFIG_CPU_FREQ */
563  
564  static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
565  {
566 @@ -205,7 +207,12 @@ int acpi_processor_ppc_has_changed(struc
567         if (ret < 0)
568                 return (ret);
569         else
570 +#ifdef CONFIG_CPU_FREQ
571                 return cpufreq_update_policy(pr->id);
572 +#elif defined(CONFIG_PROCESSOR_EXTERNAL_CONTROL)
573 +               return processor_notify_external(pr,
574 +                               PROCESSOR_PM_CHANGE, PM_TYPE_PERF);
575 +#endif
576  }
577  
578  int acpi_processor_get_bios_limit(int cpu, unsigned int *limit)
579 @@ -221,6 +228,7 @@ int acpi_processor_get_bios_limit(int cp
580  }
581  EXPORT_SYMBOL(acpi_processor_get_bios_limit);
582  
583 +#ifdef CONFIG_CPU_FREQ
584  void acpi_processor_ppc_init(void)
585  {
586         if (!cpufreq_register_notifier
587 @@ -239,6 +247,7 @@ void acpi_processor_ppc_exit(void)
588  
589         acpi_processor_ppc_status &= ~PPC_REGISTERED;
590  }
591 +#endif /* CONFIG_CPU_FREQ */
592  
593  static int acpi_processor_get_performance_control(struct acpi_processor *pr)
594  {
595 @@ -386,7 +395,10 @@ static int acpi_processor_get_performanc
596         return result;
597  }
598  
599 -static int acpi_processor_get_performance_info(struct acpi_processor *pr)
600 +#ifndef CONFIG_PROCESSOR_EXTERNAL_CONTROL
601 +static
602 +#endif
603 +int acpi_processor_get_performance_info(struct acpi_processor *pr)
604  {
605         int result = 0;
606         acpi_status status = AE_OK;
607 @@ -431,6 +443,7 @@ static int acpi_processor_get_performanc
608         return result;
609  }
610  
611 +#ifdef CONFIG_CPU_FREQ
612  int acpi_processor_notify_smm(struct module *calling_module)
613  {
614         acpi_status status;
615 @@ -491,8 +504,12 @@ int acpi_processor_notify_smm(struct mod
616  }
617  
618  EXPORT_SYMBOL(acpi_processor_notify_smm);
619 +#endif /* CONFIG_CPU_FREQ */
620  
621 -static int acpi_processor_get_psd(struct acpi_processor        *pr)
622 +#ifndef CONFIG_PROCESSOR_EXTERNAL_CONTROL
623 +static
624 +#endif
625 +int acpi_processor_get_psd(struct acpi_processor *pr)
626  {
627         int result = 0;
628         acpi_status status = AE_OK;
629 --- head-2011-10-07.orig/drivers/acpi/sleep.c   2011-10-07 10:46:17.000000000 +0200
630 +++ head-2011-10-07/drivers/acpi/sleep.c        2011-04-13 11:25:29.000000000 +0200
631 @@ -61,6 +61,7 @@ static struct notifier_block tts_notifie
632  static int acpi_sleep_prepare(u32 acpi_state)
633  {
634  #ifdef CONFIG_ACPI_SLEEP
635 +#ifndef CONFIG_ACPI_PV_SLEEP
636         /* do we have a wakeup address for S2 and S3? */
637         if (acpi_state == ACPI_STATE_S3) {
638                 if (!acpi_wakeup_address) {
639 @@ -70,6 +71,7 @@ static int acpi_sleep_prepare(u32 acpi_s
640                                 (acpi_physical_address)acpi_wakeup_address);
641  
642         }
643 +#endif
644         ACPI_FLUSH_CPU_CACHE();
645  #endif
646         printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
647 --- head-2010-03-24.orig/drivers/block/floppy.c 2010-03-24 13:43:18.000000000 +0100
648 +++ head-2010-03-24/drivers/block/floppy.c      2010-03-25 14:39:44.000000000 +0100
649 @@ -146,7 +146,9 @@
650  
651  #undef  FLOPPY_SILENT_DCL_CLEAR
652  
653 +#ifndef CONFIG_XEN
654  #define REALLY_SLOW_IO
655 +#endif
656  
657  #define DEBUGT 2
658  
659 --- head-2011-10-07.orig/drivers/char/agp/intel-gtt.c   2011-10-07 10:46:17.000000000 +0200
660 +++ head-2011-10-07/drivers/char/agp/intel-gtt.c        2011-04-13 11:25:29.000000000 +0200
661 @@ -147,8 +147,19 @@ static struct page *i8xx_alloc_pages(voi
662         if (page == NULL)
663                 return NULL;
664  
665 +#ifdef CONFIG_XEN
666 +       if (xen_create_contiguous_region((unsigned long)page_address(page), 2, 32)) {
667 +               __free_pages(page, 2);
668 +               return NULL;
669 +       }
670 +#endif
671 +
672         if (set_pages_uc(page, 4) < 0) {
673                 set_pages_wb(page, 4);
674 +#ifdef CONFIG_XEN
675 +               xen_destroy_contiguous_region((unsigned long)page_address(page),
676 +                                             2);
677 +#endif
678                 __free_pages(page, 2);
679                 return NULL;
680         }
681 @@ -163,6 +174,9 @@ static void i8xx_destroy_pages(struct pa
682                 return;
683  
684         set_pages_wb(page, 4);
685 +#ifdef CONFIG_XEN
686 +       xen_destroy_contiguous_region((unsigned long)page_address(page), 2);
687 +#endif
688         put_page(page);
689         __free_pages(page, 2);
690         atomic_dec(&agp_bridge->current_memory_agp);
691 --- head-2011-10-07.orig/drivers/char/mem.c     2011-10-07 10:46:17.000000000 +0200
692 +++ head-2011-10-07/drivers/char/mem.c  2011-05-23 10:56:58.000000000 +0200
693 @@ -86,6 +86,7 @@ void __weak unxlate_dev_mem_ptr(unsigned
694  {
695  }
696  
697 +#ifndef ARCH_HAS_DEV_MEM
698  /*
699   * This funcion reads the *physical* memory. The f_pos points directly to the
700   * memory location.
701 @@ -208,6 +209,7 @@ static ssize_t write_mem(struct file *fi
702         *ppos += written;
703         return written;
704  }
705 +#endif
706  
707  int __weak phys_mem_access_prot_allowed(struct file *file,
708         unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
709 @@ -334,6 +336,9 @@ static int mmap_mem(struct file *file, s
710  static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
711  {
712         unsigned long pfn;
713 +#ifdef CONFIG_XEN
714 +       unsigned long i, count;
715 +#endif
716  
717         /* Turn a kernel-virtual address into a physical page frame */
718         pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
719 @@ -348,6 +353,13 @@ static int mmap_kmem(struct file *file, 
720         if (!pfn_valid(pfn))
721                 return -EIO;
722  
723 +#ifdef CONFIG_XEN
724 +       count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
725 +       for (i = 0; i < count; i++)
726 +               if ((pfn + i) != mfn_to_local_pfn(pfn_to_mfn(pfn + i)))
727 +                       return -EIO;
728 +#endif
729 +
730         vma->vm_pgoff = pfn;
731         return mmap_mem(file, vma);
732  }
733 @@ -739,6 +751,7 @@ static int open_port(struct inode * inod
734  #define open_kmem      open_mem
735  #define open_oldmem    open_mem
736  
737 +#ifndef ARCH_HAS_DEV_MEM
738  static const struct file_operations mem_fops = {
739         .llseek         = memory_lseek,
740         .read           = read_mem,
741 @@ -747,6 +760,9 @@ static const struct file_operations mem_
742         .open           = open_mem,
743         .get_unmapped_area = get_unmapped_area_mem,
744  };
745 +#else
746 +extern const struct file_operations mem_fops;
747 +#endif
748  
749  #ifdef CONFIG_DEVKMEM
750  static const struct file_operations kmem_fops = {
751 --- head-2011-10-07.orig/drivers/char/tpm/Makefile      2011-10-07 10:46:17.000000000 +0200
752 +++ head-2011-10-07/drivers/char/tpm/Makefile   2011-04-13 11:25:29.000000000 +0200
753 @@ -9,3 +9,5 @@ obj-$(CONFIG_TCG_TIS) += tpm_tis.o
754  obj-$(CONFIG_TCG_NSC) += tpm_nsc.o
755  obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
756  obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
757 +obj-$(CONFIG_TCG_XEN) += tpm_xenu.o
758 +tpm_xenu-y = tpm_xen.o tpm_vtpm.o
759 --- head-2011-10-07.orig/drivers/char/tpm/tpm.h 2011-10-07 10:46:17.000000000 +0200
760 +++ head-2011-10-07/drivers/char/tpm/tpm.h      2011-09-07 14:38:52.000000000 +0200
761 @@ -115,6 +115,9 @@ struct tpm_chip {
762         struct dentry **bios_dir;
763  
764         struct list_head list;
765 +#ifdef CONFIG_XEN
766 +       void *priv;
767 +#endif
768         void (*release) (struct device *);
769  };
770  
771 @@ -279,6 +282,18 @@ struct tpm_cmd_t {
772  
773  ssize_t        tpm_getcap(struct device *, __be32, cap_t *, const char *);
774  
775 +#ifdef CONFIG_XEN
776 +static inline void *chip_get_private(const struct tpm_chip *chip)
777 +{
778 +       return chip->priv;
779 +}
780 +
781 +static inline void chip_set_private(struct tpm_chip *chip, void *priv)
782 +{
783 +       chip->priv = priv;
784 +}
785 +#endif
786 +
787  extern void tpm_get_timeouts(struct tpm_chip *);
788  extern void tpm_gen_interrupt(struct tpm_chip *);
789  extern void tpm_continue_selftest(struct tpm_chip *);
790 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
791 +++ head-2011-10-07/drivers/char/tpm/tpm_vtpm.c 2011-01-31 14:53:38.000000000 +0100
792 @@ -0,0 +1,542 @@
793 +/*
794 + * Copyright (C) 2006 IBM Corporation
795 + *
796 + * Authors:
797 + * Stefan Berger <stefanb@us.ibm.com>
798 + *
799 + * Generic device driver part for device drivers in a virtualized
800 + * environment.
801 + *
802 + * This program is free software; you can redistribute it and/or
803 + * modify it under the terms of the GNU General Public License as
804 + * published by the Free Software Foundation, version 2 of the
805 + * License.
806 + *
807 + */
808 +
809 +#include <asm/uaccess.h>
810 +#include <linux/list.h>
811 +#include <linux/device.h>
812 +#include <linux/interrupt.h>
813 +#include <linux/platform_device.h>
814 +#include "tpm.h"
815 +#include "tpm_vtpm.h"
816 +
817 +/* read status bits */
818 +enum {
819 +       STATUS_BUSY = 0x01,
820 +       STATUS_DATA_AVAIL = 0x02,
821 +       STATUS_READY = 0x04
822 +};
823 +
824 +struct transmission {
825 +       struct list_head next;
826 +
827 +       unsigned char *request;
828 +       size_t  request_len;
829 +       size_t  request_buflen;
830 +
831 +       unsigned char *response;
832 +       size_t  response_len;
833 +       size_t  response_buflen;
834 +
835 +       unsigned int flags;
836 +};
837 +
838 +enum {
839 +       TRANSMISSION_FLAG_WAS_QUEUED = 0x1
840 +};
841 +
842 +
843 +enum {
844 +       DATAEX_FLAG_QUEUED_ONLY = 0x1
845 +};
846 +
847 +
848 +/* local variables */
849 +
850 +/* local function prototypes */
851 +static int _vtpm_send_queued(struct tpm_chip *chip);
852 +
853 +
854 +/* =============================================================
855 + * Some utility functions
856 + * =============================================================
857 + */
858 +static void vtpm_state_init(struct vtpm_state *vtpms)
859 +{
860 +       vtpms->current_request = NULL;
861 +       spin_lock_init(&vtpms->req_list_lock);
862 +       init_waitqueue_head(&vtpms->req_wait_queue);
863 +       INIT_LIST_HEAD(&vtpms->queued_requests);
864 +
865 +       vtpms->current_response = NULL;
866 +       spin_lock_init(&vtpms->resp_list_lock);
867 +       init_waitqueue_head(&vtpms->resp_wait_queue);
868 +
869 +       vtpms->disconnect_time = jiffies;
870 +}
871 +
872 +
873 +static inline struct transmission *transmission_alloc(void)
874 +{
875 +       return kzalloc(sizeof(struct transmission), GFP_ATOMIC);
876 +}
877 +
878 +static unsigned char *
879 +transmission_set_req_buffer(struct transmission *t,
880 +                            unsigned char *buffer, size_t len)
881 +{
882 +       if (t->request_buflen < len) {
883 +               kfree(t->request);
884 +               t->request = kmalloc(len, GFP_KERNEL);
885 +               if (!t->request) {
886 +                       t->request_buflen = 0;
887 +                       return NULL;
888 +               }
889 +               t->request_buflen = len;
890 +       }
891 +
892 +       memcpy(t->request, buffer, len);
893 +       t->request_len = len;
894 +
895 +       return t->request;
896 +}
897 +
898 +static unsigned char *
899 +transmission_set_res_buffer(struct transmission *t,
900 +                            const unsigned char *buffer, size_t len)
901 +{
902 +       if (t->response_buflen < len) {
903 +               kfree(t->response);
904 +               t->response = kmalloc(len, GFP_ATOMIC);
905 +               if (!t->response) {
906 +                       t->response_buflen = 0;
907 +                       return NULL;
908 +               }
909 +               t->response_buflen = len;
910 +       }
911 +
912 +       memcpy(t->response, buffer, len);
913 +       t->response_len = len;
914 +
915 +       return t->response;
916 +}
917 +
918 +static inline void transmission_free(struct transmission *t)
919 +{
920 +       kfree(t->request);
921 +       kfree(t->response);
922 +       kfree(t);
923 +}
924 +
925 +/* =============================================================
926 + * Interface with the lower layer driver
927 + * =============================================================
928 + */
929 +/*
930 + * Lower layer uses this function to make a response available.
931 + */
932 +int vtpm_vd_recv(const struct tpm_chip *chip,
933 +                 const unsigned char *buffer, size_t count,
934 +                 void *ptr)
935 +{
936 +       unsigned long flags;
937 +       int ret_size = 0;
938 +       struct transmission *t;
939 +       struct vtpm_state *vtpms;
940 +
941 +       vtpms = (struct vtpm_state *)chip_get_private(chip);
942 +
943 +       /*
944 +        * The list with requests must contain one request
945 +        * only and the element there must be the one that
946 +        * was passed to me from the front-end.
947 +        */
948 +       spin_lock_irqsave(&vtpms->resp_list_lock, flags);
949 +       if (vtpms->current_request != ptr) {
950 +               spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
951 +               return 0;
952 +       }
953 +
954 +       if ((t = vtpms->current_request)) {
955 +               transmission_free(t);
956 +               vtpms->current_request = NULL;
957 +       }
958 +
959 +       t = transmission_alloc();
960 +       if (t) {
961 +               if (!transmission_set_res_buffer(t, buffer, count)) {
962 +                       transmission_free(t);
963 +                       spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
964 +                       return -ENOMEM;
965 +               }
966 +               ret_size = count;
967 +               vtpms->current_response = t;
968 +               wake_up_interruptible(&vtpms->resp_wait_queue);
969 +       }
970 +       spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
971 +
972 +       return ret_size;
973 +}
974 +
975 +
976 +/*
977 + * Lower layer indicates its status (connected/disconnected)
978 + */
979 +void vtpm_vd_status(const struct tpm_chip *chip, u8 vd_status)
980 +{
981 +       struct vtpm_state *vtpms;
982 +
983 +       vtpms = (struct vtpm_state *)chip_get_private(chip);
984 +
985 +       vtpms->vd_status = vd_status;
986 +       if ((vtpms->vd_status & TPM_VD_STATUS_CONNECTED) == 0) {
987 +               vtpms->disconnect_time = jiffies;
988 +       }
989 +}
990 +
991 +/* =============================================================
992 + * Interface with the generic TPM driver
993 + * =============================================================
994 + */
995 +static int vtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count)
996 +{
997 +       int rc = 0;
998 +       unsigned long flags;
999 +       struct vtpm_state *vtpms;
1000 +
1001 +       vtpms = (struct vtpm_state *)chip_get_private(chip);
1002 +
1003 +       /*
1004 +        * Check if the previous operation only queued the command
1005 +        * In this case there won't be a response, so I just
1006 +        * return from here and reset that flag. In any other
1007 +        * case I should receive a response from the back-end.
1008 +        */
1009 +       spin_lock_irqsave(&vtpms->resp_list_lock, flags);
1010 +       if ((vtpms->flags & DATAEX_FLAG_QUEUED_ONLY) != 0) {
1011 +               vtpms->flags &= ~DATAEX_FLAG_QUEUED_ONLY;
1012 +               spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
1013 +               /*
1014 +                * The first few commands (measurements) must be
1015 +                * queued since it might not be possible to talk to the
1016 +                * TPM, yet.
1017 +                * Return a response of up to 30 '0's.
1018 +                */
1019 +
1020 +               count = min_t(size_t, count, 30);
1021 +               memset(buf, 0x0, count);
1022 +               return count;
1023 +       }
1024 +       /*
1025 +        * Check whether something is in the responselist and if
1026 +        * there's nothing in the list wait for something to appear.
1027 +        */
1028 +
1029 +       if (!vtpms->current_response) {
1030 +               spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
1031 +               interruptible_sleep_on_timeout(&vtpms->resp_wait_queue,
1032 +                                              1000);
1033 +               spin_lock_irqsave(&vtpms->resp_list_lock ,flags);
1034 +       }
1035 +
1036 +       if (vtpms->current_response) {
1037 +               struct transmission *t = vtpms->current_response;
1038 +               vtpms->current_response = NULL;
1039 +               rc = min(count, t->response_len);
1040 +               memcpy(buf, t->response, rc);
1041 +               transmission_free(t);
1042 +       }
1043 +
1044 +       spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
1045 +       return rc;
1046 +}
1047 +
1048 +static int vtpm_send(struct tpm_chip *chip, u8 *buf, size_t count)
1049 +{
1050 +       int rc = 0;
1051 +       unsigned long flags;
1052 +       struct transmission *t = transmission_alloc();
1053 +       struct vtpm_state *vtpms;
1054 +
1055 +       vtpms = (struct vtpm_state *)chip_get_private(chip);
1056 +
1057 +       if (!t)
1058 +               return -ENOMEM;
1059 +       /*
1060 +        * If there's a current request, it must be the
1061 +        * previous request that has timed out.
1062 +        */
1063 +       spin_lock_irqsave(&vtpms->req_list_lock, flags);
1064 +       if (vtpms->current_request != NULL) {
1065 +               printk("WARNING: Sending although there is a request outstanding.\n"
1066 +                      "         Previous request must have timed out.\n");
1067 +               transmission_free(vtpms->current_request);
1068 +               vtpms->current_request = NULL;
1069 +       }
1070 +       spin_unlock_irqrestore(&vtpms->req_list_lock, flags);
1071 +
1072 +       /*
1073 +        * Queue the packet if the driver below is not
1074 +        * ready, yet, or there is any packet already
1075 +        * in the queue.
1076 +        * If the driver below is ready, unqueue all
1077 +        * packets first before sending our current
1078 +        * packet.
1079 +        * For each unqueued packet, except for the
1080 +        * last (=current) packet, call the function
1081 +        * tpm_xen_recv to wait for the response to come
1082 +        * back.
1083 +        */
1084 +       if ((vtpms->vd_status & TPM_VD_STATUS_CONNECTED) == 0) {
1085 +               if (time_after(jiffies,
1086 +                              vtpms->disconnect_time + HZ * 10)) {
1087 +                       rc = -ENOENT;
1088 +               } else {
1089 +                       goto queue_it;
1090 +               }
1091 +       } else {
1092 +               /*
1093 +                * Send all queued packets.
1094 +                */
1095 +               if (_vtpm_send_queued(chip) == 0) {
1096 +
1097 +                       vtpms->current_request = t;
1098 +
1099 +                       rc = vtpm_vd_send(vtpms->tpm_private,
1100 +                                         buf,
1101 +                                         count,
1102 +                                         t);
1103 +                       /*
1104 +                        * The generic TPM driver will call
1105 +                        * the function to receive the response.
1106 +                        */
1107 +                       if (rc < 0) {
1108 +                               vtpms->current_request = NULL;
1109 +                               goto queue_it;
1110 +                       }
1111 +               } else {
1112 +queue_it:
1113 +                       if (!transmission_set_req_buffer(t, buf, count)) {
1114 +                               transmission_free(t);
1115 +                               rc = -ENOMEM;
1116 +                               goto exit;
1117 +                       }
1118 +                       /*
1119 +                        * An error occurred. Don't event try
1120 +                        * to send the current request. Just
1121 +                        * queue it.
1122 +                        */
1123 +                       spin_lock_irqsave(&vtpms->req_list_lock, flags);
1124 +                       vtpms->flags |= DATAEX_FLAG_QUEUED_ONLY;
1125 +                       list_add_tail(&t->next, &vtpms->queued_requests);
1126 +                       spin_unlock_irqrestore(&vtpms->req_list_lock, flags);
1127 +               }
1128 +       }
1129 +
1130 +exit:
1131 +       return rc;
1132 +}
1133 +
1134 +
1135 +/*
1136 + * Send all queued requests.
1137 + */
1138 +static int _vtpm_send_queued(struct tpm_chip *chip)
1139 +{
1140 +       int rc;
1141 +       int error = 0;
1142 +       long flags;
1143 +       unsigned char buffer[1];
1144 +       struct vtpm_state *vtpms;
1145 +       vtpms = (struct vtpm_state *)chip_get_private(chip);
1146 +
1147 +       spin_lock_irqsave(&vtpms->req_list_lock, flags);
1148 +
1149 +       while (!list_empty(&vtpms->queued_requests)) {
1150 +               /*
1151 +                * Need to dequeue them.
1152 +                * Read the result into a dummy buffer.
1153 +                */
1154 +               struct transmission *qt = (struct transmission *)
1155 +                                         vtpms->queued_requests.next;
1156 +               list_del(&qt->next);
1157 +               vtpms->current_request = qt;
1158 +               spin_unlock_irqrestore(&vtpms->req_list_lock, flags);
1159 +
1160 +               rc = vtpm_vd_send(vtpms->tpm_private,
1161 +                                 qt->request,
1162 +                                 qt->request_len,
1163 +                                 qt);
1164 +
1165 +               if (rc < 0) {
1166 +                       spin_lock_irqsave(&vtpms->req_list_lock, flags);
1167 +                       if ((qt = vtpms->current_request) != NULL) {
1168 +                               /*
1169 +                                * requeue it at the beginning
1170 +                                * of the list
1171 +                                */
1172 +                               list_add(&qt->next,
1173 +                                        &vtpms->queued_requests);
1174 +                       }
1175 +                       vtpms->current_request = NULL;
1176 +                       error = 1;
1177 +                       break;
1178 +               }
1179 +               /*
1180 +                * After this point qt is not valid anymore!
1181 +                * It is freed when the front-end is delivering
1182 +                * the data by calling tpm_recv
1183 +                */
1184 +               /*
1185 +                * Receive response into provided dummy buffer
1186 +                */
1187 +               rc = vtpm_recv(chip, buffer, sizeof(buffer));
1188 +               spin_lock_irqsave(&vtpms->req_list_lock, flags);
1189 +       }
1190 +
1191 +       spin_unlock_irqrestore(&vtpms->req_list_lock, flags);
1192 +
1193 +       return error;
1194 +}
1195 +
1196 +static void vtpm_cancel(struct tpm_chip *chip)
1197 +{
1198 +       unsigned long flags;
1199 +       struct vtpm_state *vtpms = (struct vtpm_state *)chip_get_private(chip);
1200 +
1201 +       spin_lock_irqsave(&vtpms->resp_list_lock,flags);
1202 +
1203 +       if (!vtpms->current_response && vtpms->current_request) {
1204 +               spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
1205 +               interruptible_sleep_on(&vtpms->resp_wait_queue);
1206 +               spin_lock_irqsave(&vtpms->resp_list_lock,flags);
1207 +       }
1208 +
1209 +       if (vtpms->current_response) {
1210 +               struct transmission *t = vtpms->current_response;
1211 +               vtpms->current_response = NULL;
1212 +               transmission_free(t);
1213 +       }
1214 +
1215 +       spin_unlock_irqrestore(&vtpms->resp_list_lock,flags);
1216 +}
1217 +
1218 +static u8 vtpm_status(struct tpm_chip *chip)
1219 +{
1220 +       u8 rc = 0;
1221 +       unsigned long flags;
1222 +       struct vtpm_state *vtpms;
1223 +
1224 +       vtpms = (struct vtpm_state *)chip_get_private(chip);
1225 +
1226 +       spin_lock_irqsave(&vtpms->resp_list_lock, flags);
1227 +       /*
1228 +        * Data are available if:
1229 +        *  - there's a current response
1230 +        *  - the last packet was queued only (this is fake, but necessary to
1231 +        *      get the generic TPM layer to call the receive function.)
1232 +        */
1233 +       if (vtpms->current_response ||
1234 +           0 != (vtpms->flags & DATAEX_FLAG_QUEUED_ONLY)) {
1235 +               rc = STATUS_DATA_AVAIL;
1236 +       } else if (!vtpms->current_response && !vtpms->current_request) {
1237 +               rc = STATUS_READY;
1238 +       }
1239 +
1240 +       spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
1241 +       return rc;
1242 +}
1243 +
1244 +static struct file_operations vtpm_ops = {
1245 +       .owner = THIS_MODULE,
1246 +       .llseek = no_llseek,
1247 +       .open = tpm_open,
1248 +       .read = tpm_read,
1249 +       .write = tpm_write,
1250 +       .release = tpm_release,
1251 +};
1252 +
1253 +static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
1254 +static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
1255 +static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
1256 +static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
1257 +static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
1258 +static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,
1259 +                  NULL);
1260 +static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
1261 +static DEVICE_ATTR(cancel, S_IWUSR |S_IWGRP, NULL, tpm_store_cancel);
1262 +
1263 +static struct attribute *vtpm_attrs[] = {
1264 +       &dev_attr_pubek.attr,
1265 +       &dev_attr_pcrs.attr,
1266 +       &dev_attr_enabled.attr,
1267 +       &dev_attr_active.attr,
1268 +       &dev_attr_owned.attr,
1269 +       &dev_attr_temp_deactivated.attr,
1270 +       &dev_attr_caps.attr,
1271 +       &dev_attr_cancel.attr,
1272 +       NULL,
1273 +};
1274 +
1275 +static struct attribute_group vtpm_attr_grp = { .attrs = vtpm_attrs };
1276 +
1277 +#define TPM_LONG_TIMEOUT   (10 * 60 * HZ)
1278 +
1279 +static struct tpm_vendor_specific tpm_vtpm = {
1280 +       .recv = vtpm_recv,
1281 +       .send = vtpm_send,
1282 +       .cancel = vtpm_cancel,
1283 +       .status = vtpm_status,
1284 +       .req_complete_mask = STATUS_BUSY | STATUS_DATA_AVAIL,
1285 +       .req_complete_val  = STATUS_DATA_AVAIL,
1286 +       .req_canceled = STATUS_READY,
1287 +       .attr_group = &vtpm_attr_grp,
1288 +       .miscdev = {
1289 +               .fops = &vtpm_ops,
1290 +       },
1291 +       .duration = {
1292 +               TPM_LONG_TIMEOUT,
1293 +               TPM_LONG_TIMEOUT,
1294 +               TPM_LONG_TIMEOUT,
1295 +       },
1296 +};
1297 +
1298 +struct tpm_chip *init_vtpm(struct device *dev,
1299 +                           struct tpm_private *tp)
1300 +{
1301 +       long rc;
1302 +       struct tpm_chip *chip;
1303 +       struct vtpm_state *vtpms;
1304 +
1305 +       vtpms = kzalloc(sizeof(struct vtpm_state), GFP_KERNEL);
1306 +       if (!vtpms)
1307 +               return ERR_PTR(-ENOMEM);
1308 +
1309 +       vtpm_state_init(vtpms);
1310 +       vtpms->tpm_private = tp;
1311 +
1312 +       chip = tpm_register_hardware(dev, &tpm_vtpm);
1313 +       if (!chip) {
1314 +               rc = -ENODEV;
1315 +               goto err_free_mem;
1316 +       }
1317 +
1318 +       chip_set_private(chip, vtpms);
1319 +
1320 +       return chip;
1321 +
1322 +err_free_mem:
1323 +       kfree(vtpms);
1324 +
1325 +       return ERR_PTR(rc);
1326 +}
1327 +
1328 +void cleanup_vtpm(struct device *dev)
1329 +{
1330 +       struct tpm_chip *chip = dev_get_drvdata(dev);
1331 +       struct vtpm_state *vtpms = (struct vtpm_state*)chip_get_private(chip);
1332 +       tpm_remove_hardware(dev);
1333 +       kfree(vtpms);
1334 +}
1335 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
1336 +++ head-2011-10-07/drivers/char/tpm/tpm_vtpm.h 2011-01-31 14:53:38.000000000 +0100
1337 @@ -0,0 +1,55 @@
1338 +#ifndef TPM_VTPM_H
1339 +#define TPM_VTPM_H
1340 +
1341 +struct tpm_chip;
1342 +struct tpm_private;
1343 +
1344 +struct vtpm_state {
1345 +       struct transmission *current_request;
1346 +       spinlock_t           req_list_lock;
1347 +       wait_queue_head_t    req_wait_queue;
1348 +
1349 +       struct list_head     queued_requests;
1350 +
1351 +       struct transmission *current_response;
1352 +       spinlock_t           resp_list_lock;
1353 +       wait_queue_head_t    resp_wait_queue;     // processes waiting for responses
1354 +
1355 +       u8                   vd_status;
1356 +       u8                   flags;
1357 +
1358 +       unsigned long        disconnect_time;
1359 +
1360 +       /*
1361 +        * The following is a private structure of the underlying
1362 +        * driver. It is passed as parameter in the send function.
1363 +        */
1364 +       struct tpm_private *tpm_private;
1365 +};
1366 +
1367 +
1368 +enum vdev_status {
1369 +       TPM_VD_STATUS_DISCONNECTED = 0x0,
1370 +       TPM_VD_STATUS_CONNECTED = 0x1
1371 +};
1372 +
1373 +/* this function is called from tpm_vtpm.c */
1374 +int vtpm_vd_send(struct tpm_private * tp,
1375 +                 const u8 * buf, size_t count, void *ptr);
1376 +
1377 +/* these functions are offered by tpm_vtpm.c */
1378 +struct tpm_chip *init_vtpm(struct device *,
1379 +                           struct tpm_private *);
1380 +void cleanup_vtpm(struct device *);
1381 +int vtpm_vd_recv(const struct tpm_chip* chip,
1382 +                 const unsigned char *buffer, size_t count, void *ptr);
1383 +void vtpm_vd_status(const struct tpm_chip *, u8 status);
1384 +
1385 +static inline struct tpm_private *tpm_private_from_dev(struct device *dev)
1386 +{
1387 +       struct tpm_chip *chip = dev_get_drvdata(dev);
1388 +       struct vtpm_state *vtpms = chip_get_private(chip);
1389 +       return vtpms->tpm_private;
1390 +}
1391 +
1392 +#endif
1393 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
1394 +++ head-2011-10-07/drivers/char/tpm/tpm_xen.c  2011-01-31 14:53:38.000000000 +0100
1395 @@ -0,0 +1,722 @@
1396 +/*
1397 + * Copyright (c) 2005, IBM Corporation
1398 + *
1399 + * Author: Stefan Berger, stefanb@us.ibm.com
1400 + * Grant table support: Mahadevan Gomathisankaran
1401 + *
1402 + * This code has been derived from drivers/xen/netfront/netfront.c
1403 + *
1404 + * Copyright (c) 2002-2004, K A Fraser
1405 + *
1406 + * This program is free software; you can redistribute it and/or
1407 + * modify it under the terms of the GNU General Public License version 2
1408 + * as published by the Free Software Foundation; or, when distributed
1409 + * separately from the Linux kernel or incorporated into other
1410 + * software packages, subject to the following license:
1411 + *
1412 + * Permission is hereby granted, free of charge, to any person obtaining a copy
1413 + * of this source file (the "Software"), to deal in the Software without
1414 + * restriction, including without limitation the rights to use, copy, modify,
1415 + * merge, publish, distribute, sublicense, and/or sell copies of the Software,
1416 + * and to permit persons to whom the Software is furnished to do so, subject to
1417 + * the following conditions:
1418 + *
1419 + * The above copyright notice and this permission notice shall be included in
1420 + * all copies or substantial portions of the Software.
1421 + *
1422 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1423 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1424 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1425 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1426 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1427 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
1428 + * IN THE SOFTWARE.
1429 + */
1430 +
1431 +#include <linux/errno.h>
1432 +#include <linux/err.h>
1433 +#include <linux/interrupt.h>
1434 +#include <linux/mutex.h>
1435 +#include <asm/uaccess.h>
1436 +#include <xen/evtchn.h>
1437 +#include <xen/interface/grant_table.h>
1438 +#include <xen/interface/io/tpmif.h>
1439 +#include <xen/gnttab.h>
1440 +#include <xen/xenbus.h>
1441 +#include "tpm.h"
1442 +#include "tpm_vtpm.h"
1443 +
1444 +#undef DEBUG
1445 +
1446 +/* local structures */
1447 +struct tpm_private {
1448 +       struct tpm_chip *chip;
1449 +
1450 +       tpmif_tx_interface_t *tx;
1451 +       atomic_t refcnt;
1452 +       unsigned int irq;
1453 +       u8 is_connected;
1454 +       u8 is_suspended;
1455 +
1456 +       spinlock_t tx_lock;
1457 +
1458 +       struct tx_buffer *tx_buffers[TPMIF_TX_RING_SIZE];
1459 +
1460 +       atomic_t tx_busy;
1461 +       void *tx_remember;
1462 +
1463 +       domid_t backend_id;
1464 +       wait_queue_head_t wait_q;
1465 +
1466 +       struct xenbus_device *dev;
1467 +       int ring_ref;
1468 +};
1469 +
1470 +struct tx_buffer {
1471 +       unsigned int size;      // available space in data
1472 +       unsigned int len;       // used space in data
1473 +       unsigned char *data;    // pointer to a page
1474 +};
1475 +
1476 +
1477 +/* locally visible variables */
1478 +static grant_ref_t gref_head;
1479 +static struct tpm_private *my_priv;
1480 +
1481 +/* local function prototypes */
1482 +static irqreturn_t tpmif_int(int irq,
1483 +                             void *tpm_priv,
1484 +                             struct pt_regs *ptregs);
1485 +static void tpmif_rx_action(unsigned long unused);
1486 +static int tpmif_connect(struct xenbus_device *dev,
1487 +                         struct tpm_private *tp,
1488 +                         domid_t domid);
1489 +static DECLARE_TASKLET(tpmif_rx_tasklet, tpmif_rx_action, 0);
1490 +static int tpmif_allocate_tx_buffers(struct tpm_private *tp);
1491 +static void tpmif_free_tx_buffers(struct tpm_private *tp);
1492 +static void tpmif_set_connected_state(struct tpm_private *tp,
1493 +                                      u8 newstate);
1494 +static int tpm_xmit(struct tpm_private *tp,
1495 +                    const u8 * buf, size_t count, int userbuffer,
1496 +                    void *remember);
1497 +static void destroy_tpmring(struct tpm_private *tp);
1498 +void __exit tpmif_exit(void);
1499 +
1500 +#define DPRINTK(fmt, args...) \
1501 +    pr_debug("xen_tpm_fr (%s:%d) " fmt, __FUNCTION__, __LINE__, ##args)
1502 +#define IPRINTK(fmt, args...) \
1503 +    printk(KERN_INFO "xen_tpm_fr: " fmt, ##args)
1504 +#define WPRINTK(fmt, args...) \
1505 +    printk(KERN_WARNING "xen_tpm_fr: " fmt, ##args)
1506 +
1507 +#define GRANT_INVALID_REF      0
1508 +
1509 +
1510 +static inline int
1511 +tx_buffer_copy(struct tx_buffer *txb, const u8 *src, int len,
1512 +               int isuserbuffer)
1513 +{
1514 +       int copied = len;
1515 +
1516 +       if (len > txb->size)
1517 +               copied = txb->size;
1518 +       if (isuserbuffer) {
1519 +               if (copy_from_user(txb->data, src, copied))
1520 +                       return -EFAULT;
1521 +       } else {
1522 +               memcpy(txb->data, src, copied);
1523 +       }
1524 +       txb->len = len;
1525 +       return copied;
1526 +}
1527 +
1528 +static inline struct tx_buffer *tx_buffer_alloc(void)
1529 +{
1530 +       struct tx_buffer *txb;
1531 +
1532 +       txb = kzalloc(sizeof(struct tx_buffer), GFP_KERNEL);
1533 +       if (!txb)
1534 +               return NULL;
1535 +
1536 +       txb->len = 0;
1537 +       txb->size = PAGE_SIZE;
1538 +       txb->data = (unsigned char *)__get_free_page(GFP_KERNEL);
1539 +       if (txb->data == NULL) {
1540 +               kfree(txb);
1541 +               txb = NULL;
1542 +       }
1543 +
1544 +       return txb;
1545 +}
1546 +
1547 +
1548 +static inline void tx_buffer_free(struct tx_buffer *txb)
1549 +{
1550 +       if (txb) {
1551 +               free_page((long)txb->data);
1552 +               kfree(txb);
1553 +       }
1554 +}
1555 +
1556 +/**************************************************************
1557 + Utility function for the tpm_private structure
1558 +**************************************************************/
1559 +static void tpm_private_init(struct tpm_private *tp)
1560 +{
1561 +       spin_lock_init(&tp->tx_lock);
1562 +       init_waitqueue_head(&tp->wait_q);
1563 +       atomic_set(&tp->refcnt, 1);
1564 +}
1565 +
1566 +static void tpm_private_put(void)
1567 +{
1568 +       if (!atomic_dec_and_test(&my_priv->refcnt))
1569 +               return;
1570 +
1571 +       tpmif_free_tx_buffers(my_priv);
1572 +       kfree(my_priv);
1573 +       my_priv = NULL;
1574 +}
1575 +
1576 +static struct tpm_private *tpm_private_get(void)
1577 +{
1578 +       int err;
1579 +
1580 +       if (my_priv) {
1581 +               atomic_inc(&my_priv->refcnt);
1582 +               return my_priv;
1583 +       }
1584 +
1585 +       my_priv = kzalloc(sizeof(struct tpm_private), GFP_KERNEL);
1586 +       if (!my_priv)
1587 +               return NULL;
1588 +
1589 +       tpm_private_init(my_priv);
1590 +       err = tpmif_allocate_tx_buffers(my_priv);
1591 +       if (err < 0)
1592 +               tpm_private_put();
1593 +
1594 +       return my_priv;
1595 +}
1596 +
1597 +/**************************************************************
1598 +
1599 + The interface to let the tpm plugin register its callback
1600 + function and send data to another partition using this module
1601 +
1602 +**************************************************************/
1603 +
1604 +static DEFINE_MUTEX(suspend_lock);
1605 +/*
1606 + * Send data via this module by calling this function
1607 + */
1608 +int vtpm_vd_send(struct tpm_private *tp,
1609 +                 const u8 * buf, size_t count, void *ptr)
1610 +{
1611 +       int sent;
1612 +
1613 +       mutex_lock(&suspend_lock);
1614 +       sent = tpm_xmit(tp, buf, count, 0, ptr);
1615 +       mutex_unlock(&suspend_lock);
1616 +
1617 +       return sent;
1618 +}
1619 +
1620 +/**************************************************************
1621 + XENBUS support code
1622 +**************************************************************/
1623 +
1624 +static int setup_tpmring(struct xenbus_device *dev,
1625 +                         struct tpm_private *tp)
1626 +{
1627 +       tpmif_tx_interface_t *sring;
1628 +       int err;
1629 +
1630 +       tp->ring_ref = GRANT_INVALID_REF;
1631 +
1632 +       sring = (void *)__get_free_page(GFP_KERNEL);
1633 +       if (!sring) {
1634 +               xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
1635 +               return -ENOMEM;
1636 +       }
1637 +       tp->tx = sring;
1638 +
1639 +       err = xenbus_grant_ring(dev, virt_to_mfn(tp->tx));
1640 +       if (err < 0) {
1641 +               free_page((unsigned long)sring);
1642 +               tp->tx = NULL;
1643 +               xenbus_dev_fatal(dev, err, "allocating grant reference");
1644 +               goto fail;
1645 +       }
1646 +       tp->ring_ref = err;
1647 +
1648 +       err = tpmif_connect(dev, tp, dev->otherend_id);
1649 +       if (err)
1650 +               goto fail;
1651 +
1652 +       return 0;
1653 +fail:
1654 +       destroy_tpmring(tp);
1655 +       return err;
1656 +}
1657 +
1658 +
1659 +static void destroy_tpmring(struct tpm_private *tp)
1660 +{
1661 +       tpmif_set_connected_state(tp, 0);
1662 +
1663 +       if (tp->ring_ref != GRANT_INVALID_REF) {
1664 +               gnttab_end_foreign_access(tp->ring_ref, (unsigned long)tp->tx);
1665 +               tp->ring_ref = GRANT_INVALID_REF;
1666 +               tp->tx = NULL;
1667 +       }
1668 +
1669 +       if (tp->irq)
1670 +               unbind_from_irqhandler(tp->irq, tp);
1671 +
1672 +       tp->irq = 0;
1673 +}
1674 +
1675 +
1676 +static int talk_to_backend(struct xenbus_device *dev,
1677 +                           struct tpm_private *tp)
1678 +{
1679 +       const char *message = NULL;
1680 +       int err;
1681 +       struct xenbus_transaction xbt;
1682 +
1683 +       err = setup_tpmring(dev, tp);
1684 +       if (err) {
1685 +               xenbus_dev_fatal(dev, err, "setting up ring");
1686 +               goto out;
1687 +       }
1688 +
1689 +again:
1690 +       err = xenbus_transaction_start(&xbt);
1691 +       if (err) {
1692 +               xenbus_dev_fatal(dev, err, "starting transaction");
1693 +               goto destroy_tpmring;
1694 +       }
1695 +
1696 +       err = xenbus_printf(xbt, dev->nodename,
1697 +                           "ring-ref","%u", tp->ring_ref);
1698 +       if (err) {
1699 +               message = "writing ring-ref";
1700 +               goto abort_transaction;
1701 +       }
1702 +
1703 +       err = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
1704 +                           irq_to_evtchn_port(tp->irq));
1705 +       if (err) {
1706 +               message = "writing event-channel";
1707 +               goto abort_transaction;
1708 +       }
1709 +
1710 +       err = xenbus_transaction_end(xbt, 0);
1711 +       if (err == -EAGAIN)
1712 +               goto again;
1713 +       if (err) {
1714 +               xenbus_dev_fatal(dev, err, "completing transaction");
1715 +               goto destroy_tpmring;
1716 +       }
1717 +
1718 +       xenbus_switch_state(dev, XenbusStateConnected);
1719 +
1720 +       return 0;
1721 +
1722 +abort_transaction:
1723 +       xenbus_transaction_end(xbt, 1);
1724 +       if (message)
1725 +               xenbus_dev_error(dev, err, "%s", message);
1726 +destroy_tpmring:
1727 +       destroy_tpmring(tp);
1728 +out:
1729 +       return err;
1730 +}
1731 +
1732 +/**
1733 + * Callback received when the backend's state changes.
1734 + */
1735 +static void backend_changed(struct xenbus_device *dev,
1736 +                           enum xenbus_state backend_state)
1737 +{
1738 +       struct tpm_private *tp = tpm_private_from_dev(&dev->dev);
1739 +       DPRINTK("\n");
1740 +
1741 +       switch (backend_state) {
1742 +       case XenbusStateInitialising:
1743 +       case XenbusStateInitWait:
1744 +       case XenbusStateInitialised:
1745 +       case XenbusStateReconfiguring:
1746 +       case XenbusStateReconfigured:
1747 +       case XenbusStateUnknown:
1748 +               break;
1749 +
1750 +       case XenbusStateConnected:
1751 +               tpmif_set_connected_state(tp, 1);
1752 +               break;
1753 +
1754 +       case XenbusStateClosing:
1755 +               tpmif_set_connected_state(tp, 0);
1756 +               xenbus_frontend_closed(dev);
1757 +               break;
1758 +
1759 +       case XenbusStateClosed:
1760 +               tpmif_set_connected_state(tp, 0);
1761 +               if (tp->is_suspended == 0)
1762 +                       device_unregister(&dev->dev);
1763 +               xenbus_frontend_closed(dev);
1764 +               break;
1765 +       }
1766 +}
1767 +
1768 +static int tpmfront_probe(struct xenbus_device *dev,
1769 +                          const struct xenbus_device_id *id)
1770 +{
1771 +       int err;
1772 +       int handle;
1773 +       struct tpm_private *tp = tpm_private_get();
1774 +
1775 +       if (!tp)
1776 +               return -ENOMEM;
1777 +
1778 +       tp->chip = init_vtpm(&dev->dev, tp);
1779 +       if (IS_ERR(tp->chip))
1780 +               return PTR_ERR(tp->chip);
1781 +
1782 +       err = xenbus_scanf(XBT_NIL, dev->nodename,
1783 +                          "handle", "%i", &handle);
1784 +       if (XENBUS_EXIST_ERR(err))
1785 +               return err;
1786 +
1787 +       if (err < 0) {
1788 +               xenbus_dev_fatal(dev,err,"reading virtual-device");
1789 +               return err;
1790 +       }
1791 +
1792 +       tp->dev = dev;
1793 +
1794 +       err = talk_to_backend(dev, tp);
1795 +       if (err) {
1796 +               tpm_private_put();
1797 +               return err;
1798 +       }
1799 +
1800 +       return 0;
1801 +}
1802 +
1803 +
1804 +static int tpmfront_remove(struct xenbus_device *dev)
1805 +{
1806 +       struct tpm_private *tp = tpm_private_from_dev(&dev->dev);
1807 +       destroy_tpmring(tp);
1808 +       cleanup_vtpm(&dev->dev);
1809 +       return 0;
1810 +}
1811 +
1812 +static int tpmfront_suspend(struct xenbus_device *dev)
1813 +{
1814 +       struct tpm_private *tp = tpm_private_from_dev(&dev->dev);
1815 +       u32 ctr;
1816 +
1817 +       /* Take the lock, preventing any application from sending. */
1818 +       mutex_lock(&suspend_lock);
1819 +       tp->is_suspended = 1;
1820 +
1821 +       for (ctr = 0; atomic_read(&tp->tx_busy); ctr++) {
1822 +               if ((ctr % 10) == 0)
1823 +                       printk("TPM-FE [INFO]: Waiting for outstanding "
1824 +                              "request.\n");
1825 +               /* Wait for a request to be responded to. */
1826 +               interruptible_sleep_on_timeout(&tp->wait_q, 100);
1827 +       }
1828 +
1829 +       return 0;
1830 +}
1831 +
1832 +static int tpmfront_suspend_finish(struct tpm_private *tp)
1833 +{
1834 +       tp->is_suspended = 0;
1835 +       /* Allow applications to send again. */
1836 +       mutex_unlock(&suspend_lock);
1837 +       return 0;
1838 +}
1839 +
1840 +static int tpmfront_suspend_cancel(struct xenbus_device *dev)
1841 +{
1842 +       struct tpm_private *tp = tpm_private_from_dev(&dev->dev);
1843 +       return tpmfront_suspend_finish(tp);
1844 +}
1845 +
1846 +static int tpmfront_resume(struct xenbus_device *dev)
1847 +{
1848 +       struct tpm_private *tp = tpm_private_from_dev(&dev->dev);
1849 +       destroy_tpmring(tp);
1850 +       return talk_to_backend(dev, tp);
1851 +}
1852 +
1853 +static int tpmif_connect(struct xenbus_device *dev,
1854 +                         struct tpm_private *tp,
1855 +                         domid_t domid)
1856 +{
1857 +       int err;
1858 +
1859 +       tp->backend_id = domid;
1860 +
1861 +       err = bind_listening_port_to_irqhandler(
1862 +               domid, tpmif_int, SA_SAMPLE_RANDOM, "tpmif", tp);
1863 +       if (err <= 0) {
1864 +               WPRINTK("bind_listening_port_to_irqhandler failed "
1865 +                       "(err=%d)\n", err);
1866 +               return err;
1867 +       }
1868 +       tp->irq = err;
1869 +
1870 +       return 0;
1871 +}
1872 +
1873 +static struct xenbus_device_id tpmfront_ids[] = {
1874 +       { "vtpm" },
1875 +       { "" }
1876 +};
1877 +
1878 +static struct xenbus_driver tpmfront = {
1879 +       .name = "vtpm",
1880 +       .owner = THIS_MODULE,
1881 +       .ids = tpmfront_ids,
1882 +       .probe = tpmfront_probe,
1883 +       .remove =  tpmfront_remove,
1884 +       .resume = tpmfront_resume,
1885 +       .otherend_changed = backend_changed,
1886 +       .suspend = tpmfront_suspend,
1887 +       .suspend_cancel = tpmfront_suspend_cancel,
1888 +};
1889 +
1890 +static void __init init_tpm_xenbus(void)
1891 +{
1892 +       xenbus_register_frontend(&tpmfront);
1893 +}
1894 +
1895 +static int tpmif_allocate_tx_buffers(struct tpm_private *tp)
1896 +{
1897 +       unsigned int i;
1898 +
1899 +       for (i = 0; i < TPMIF_TX_RING_SIZE; i++) {
1900 +               tp->tx_buffers[i] = tx_buffer_alloc();
1901 +               if (!tp->tx_buffers[i]) {
1902 +                       tpmif_free_tx_buffers(tp);
1903 +                       return -ENOMEM;
1904 +               }
1905 +       }
1906 +       return 0;
1907 +}
1908 +
1909 +static void tpmif_free_tx_buffers(struct tpm_private *tp)
1910 +{
1911 +       unsigned int i;
1912 +
1913 +       for (i = 0; i < TPMIF_TX_RING_SIZE; i++)
1914 +               tx_buffer_free(tp->tx_buffers[i]);
1915 +}
1916 +
1917 +static void tpmif_rx_action(unsigned long priv)
1918 +{
1919 +       struct tpm_private *tp = (struct tpm_private *)priv;
1920 +       int i = 0;
1921 +       unsigned int received;
1922 +       unsigned int offset = 0;
1923 +       u8 *buffer;
1924 +       tpmif_tx_request_t *tx = &tp->tx->ring[i].req;
1925 +
1926 +       atomic_set(&tp->tx_busy, 0);
1927 +       wake_up_interruptible(&tp->wait_q);
1928 +
1929 +       received = tx->size;
1930 +
1931 +       buffer = kmalloc(received, GFP_ATOMIC);
1932 +       if (!buffer)
1933 +               return;
1934 +
1935 +       for (i = 0; i < TPMIF_TX_RING_SIZE && offset < received; i++) {
1936 +               struct tx_buffer *txb = tp->tx_buffers[i];
1937 +               tpmif_tx_request_t *tx;
1938 +               unsigned int tocopy;
1939 +
1940 +               tx = &tp->tx->ring[i].req;
1941 +               tocopy = tx->size;
1942 +               if (tocopy > PAGE_SIZE)
1943 +                       tocopy = PAGE_SIZE;
1944 +
1945 +               memcpy(&buffer[offset], txb->data, tocopy);
1946 +
1947 +               gnttab_release_grant_reference(&gref_head, tx->ref);
1948 +
1949 +               offset += tocopy;
1950 +       }
1951 +
1952 +       vtpm_vd_recv(tp->chip, buffer, received, tp->tx_remember);
1953 +       kfree(buffer);
1954 +}
1955 +
1956 +
1957 +static irqreturn_t tpmif_int(int irq, void *tpm_priv, struct pt_regs *ptregs)
1958 +{
1959 +       struct tpm_private *tp = tpm_priv;
1960 +       unsigned long flags;
1961 +
1962 +       spin_lock_irqsave(&tp->tx_lock, flags);
1963 +       tpmif_rx_tasklet.data = (unsigned long)tp;
1964 +       tasklet_schedule(&tpmif_rx_tasklet);
1965 +       spin_unlock_irqrestore(&tp->tx_lock, flags);
1966 +
1967 +       return IRQ_HANDLED;
1968 +}
1969 +
1970 +
1971 +static int tpm_xmit(struct tpm_private *tp,
1972 +                    const u8 * buf, size_t count, int isuserbuffer,
1973 +                    void *remember)
1974 +{
1975 +       tpmif_tx_request_t *tx;
1976 +       TPMIF_RING_IDX i;
1977 +       unsigned int offset = 0;
1978 +
1979 +       spin_lock_irq(&tp->tx_lock);
1980 +
1981 +       if (unlikely(atomic_read(&tp->tx_busy))) {
1982 +               printk("tpm_xmit: There's an outstanding request/response "
1983 +                      "on the way!\n");
1984 +               spin_unlock_irq(&tp->tx_lock);
1985 +               return -EBUSY;
1986 +       }
1987 +
1988 +       if (tp->is_connected != 1) {
1989 +               spin_unlock_irq(&tp->tx_lock);
1990 +               return -EIO;
1991 +       }
1992 +
1993 +       for (i = 0; count > 0 && i < TPMIF_TX_RING_SIZE; i++) {
1994 +               struct tx_buffer *txb = tp->tx_buffers[i];
1995 +               int copied;
1996 +
1997 +               if (!txb) {
1998 +                       DPRINTK("txb (i=%d) is NULL. buffers initilized?\n"
1999 +                               "Not transmitting anything!\n", i);
2000 +                       spin_unlock_irq(&tp->tx_lock);
2001 +                       return -EFAULT;
2002 +               }
2003 +
2004 +               copied = tx_buffer_copy(txb, &buf[offset], count,
2005 +                                       isuserbuffer);
2006 +               if (copied < 0) {
2007 +                       /* An error occurred */
2008 +                       spin_unlock_irq(&tp->tx_lock);
2009 +                       return copied;
2010 +               }
2011 +               count -= copied;
2012 +               offset += copied;
2013 +
2014 +               tx = &tp->tx->ring[i].req;
2015 +               tx->addr = virt_to_machine(txb->data);
2016 +               tx->size = txb->len;
2017 +               tx->unused = 0;
2018 +
2019 +               DPRINTK("First 4 characters sent by TPM-FE are "
2020 +                       "0x%02x 0x%02x 0x%02x 0x%02x\n",
2021 +                       txb->data[0],txb->data[1],txb->data[2],txb->data[3]);
2022 +
2023 +               /* Get the granttable reference for this page. */
2024 +               tx->ref = gnttab_claim_grant_reference(&gref_head);
2025 +               if (tx->ref == -ENOSPC) {
2026 +                       spin_unlock_irq(&tp->tx_lock);
2027 +                       DPRINTK("Grant table claim reference failed in "
2028 +                               "func:%s line:%d file:%s\n",
2029 +                               __FUNCTION__, __LINE__, __FILE__);
2030 +                       return -ENOSPC;
2031 +               }
2032 +               gnttab_grant_foreign_access_ref(tx->ref,
2033 +                                               tp->backend_id,
2034 +                                               virt_to_mfn(txb->data),
2035 +                                               0 /*RW*/);
2036 +               wmb();
2037 +       }
2038 +
2039 +       atomic_set(&tp->tx_busy, 1);
2040 +       tp->tx_remember = remember;
2041 +
2042 +       mb();
2043 +
2044 +       notify_remote_via_irq(tp->irq);
2045 +
2046 +       spin_unlock_irq(&tp->tx_lock);
2047 +       return offset;
2048 +}
2049 +
2050 +
2051 +static void tpmif_notify_upperlayer(struct tpm_private *tp)
2052 +{
2053 +       /* Notify upper layer about the state of the connection to the BE. */
2054 +       vtpm_vd_status(tp->chip, (tp->is_connected
2055 +                                 ? TPM_VD_STATUS_CONNECTED
2056 +                                 : TPM_VD_STATUS_DISCONNECTED));
2057 +}
2058 +
2059 +
2060 +static void tpmif_set_connected_state(struct tpm_private *tp, u8 is_connected)
2061 +{
2062 +       /*
2063 +        * Don't notify upper layer if we are in suspend mode and
2064 +        * should disconnect - assumption is that we will resume
2065 +        * The mutex keeps apps from sending.
2066 +        */
2067 +       if (is_connected == 0 && tp->is_suspended == 1)
2068 +               return;
2069 +
2070 +       /*
2071 +        * Unlock the mutex if we are connected again
2072 +        * after being suspended - now resuming.
2073 +        * This also removes the suspend state.
2074 +        */
2075 +       if (is_connected == 1 && tp->is_suspended == 1)
2076 +               tpmfront_suspend_finish(tp);
2077 +
2078 +       if (is_connected != tp->is_connected) {
2079 +               tp->is_connected = is_connected;
2080 +               tpmif_notify_upperlayer(tp);
2081 +       }
2082 +}
2083 +
2084 +
2085 +
2086 +/* =================================================================
2087 + * Initialization function.
2088 + * =================================================================
2089 + */
2090 +
2091 +
2092 +static int __init tpmif_init(void)
2093 +{
2094 +       struct tpm_private *tp;
2095 +
2096 +       if (is_initial_xendomain())
2097 +               return -EPERM;
2098 +
2099 +       tp = tpm_private_get();
2100 +       if (!tp)
2101 +               return -ENOMEM;
2102 +
2103 +       IPRINTK("Initialising the vTPM driver.\n");
2104 +       if (gnttab_alloc_grant_references(TPMIF_TX_RING_SIZE,
2105 +                                         &gref_head) < 0) {
2106 +               tpm_private_put();
2107 +               return -EFAULT;
2108 +       }
2109 +
2110 +       init_tpm_xenbus();
2111 +       return 0;
2112 +}
2113 +
2114 +
2115 +module_init(tpmif_init);
2116 +
2117 +MODULE_LICENSE("Dual BSD/GPL");
2118 --- head-2011-10-07.orig/drivers/edac/edac_mc.c 2011-10-07 10:46:17.000000000 +0200
2119 +++ head-2011-10-07/drivers/edac/edac_mc.c      2011-06-30 15:37:01.000000000 +0200
2120 @@ -611,6 +611,10 @@ static void edac_mc_scrub_block(unsigned
2121  
2122         debugf3("%s()\n", __func__);
2123  
2124 +#ifdef CONFIG_XEN
2125 +       page = mfn_to_local_pfn(page);
2126 +#endif
2127 +
2128         /* ECC error page was not in our memory. Ignore it. */
2129         if (!pfn_valid(page))
2130                 return;
2131 --- head-2011-10-07.orig/drivers/firmware/dell_rbu.c    2011-10-07 10:46:17.000000000 +0200
2132 +++ head-2011-10-07/drivers/firmware/dell_rbu.c 2011-04-13 11:25:29.000000000 +0200
2133 @@ -170,9 +170,28 @@ static int create_packet(void *data, siz
2134                         spin_lock(&rbu_data.lock);
2135                         goto out_alloc_packet_array;
2136                 }
2137 +#ifdef CONFIG_XEN
2138 +               if (ordernum && xen_create_contiguous_region(
2139 +                       (unsigned long)packet_data_temp_buf, ordernum, 0)) {
2140 +                       free_pages((unsigned long)packet_data_temp_buf,
2141 +                                  ordernum);
2142 +                       printk(KERN_WARNING
2143 +                               "dell_rbu:%s: failed to adjust new "
2144 +                               "packet\n", __func__);
2145 +                       retval = -ENOMEM;
2146 +                       spin_lock(&rbu_data.lock);
2147 +                       goto out_alloc_packet_array;
2148 +               }
2149 +#endif
2150  
2151 -               if ((unsigned long)virt_to_phys(packet_data_temp_buf)
2152 +               if ((unsigned long)virt_to_bus(packet_data_temp_buf)
2153                                 < allocation_floor) {
2154 +#ifdef CONFIG_XEN
2155 +                       if (ordernum)
2156 +                               xen_destroy_contiguous_region(
2157 +                                       (unsigned long)packet_data_temp_buf,
2158 +                                       ordernum);
2159 +#endif
2160                         pr_debug("packet 0x%lx below floor at 0x%lx.\n",
2161                                         (unsigned long)virt_to_phys(
2162                                                 packet_data_temp_buf),
2163 @@ -186,7 +205,7 @@ static int create_packet(void *data, siz
2164         newpacket->data = packet_data_temp_buf;
2165  
2166         pr_debug("create_packet: newpacket at physical addr %lx\n",
2167 -               (unsigned long)virt_to_phys(newpacket->data));
2168 +               (unsigned long)virt_to_bus(newpacket->data));
2169  
2170         /* packets may not have fixed size */
2171         newpacket->length = length;
2172 @@ -205,7 +224,7 @@ out_alloc_packet_array:
2173         /* always free packet array */
2174         for (;idx>0;idx--) {
2175                 pr_debug("freeing unused packet below floor 0x%lx.\n",
2176 -                       (unsigned long)virt_to_phys(
2177 +                       (unsigned long)virt_to_bus(
2178                                 invalid_addr_packet_array[idx-1]));
2179                 free_pages((unsigned long)invalid_addr_packet_array[idx-1],
2180                         ordernum);
2181 @@ -349,6 +368,13 @@ static void packet_empty_list(void)
2182                  * to make sure there are no stale RBU packets left in memory
2183                  */
2184                 memset(newpacket->data, 0, rbu_data.packetsize);
2185 +#ifdef CONFIG_XEN
2186 +               if (newpacket->ordernum)
2187 +                       xen_destroy_contiguous_region(
2188 +                               (unsigned long)newpacket->data,
2189 +                               newpacket->ordernum);
2190 +#endif
2191 +
2192                 free_pages((unsigned long) newpacket->data,
2193                         newpacket->ordernum);
2194                 kfree(newpacket);
2195 @@ -403,7 +429,9 @@ static int img_update_realloc(unsigned l
2196  {
2197         unsigned char *image_update_buffer = NULL;
2198         unsigned long rc;
2199 +#ifndef CONFIG_XEN
2200         unsigned long img_buf_phys_addr;
2201 +#endif
2202         int ordernum;
2203         int dma_alloc = 0;
2204  
2205 @@ -434,15 +462,19 @@ static int img_update_realloc(unsigned l
2206  
2207         spin_unlock(&rbu_data.lock);
2208  
2209 +#ifndef CONFIG_XEN
2210         ordernum = get_order(size);
2211         image_update_buffer =
2212                 (unsigned char *) __get_free_pages(GFP_KERNEL, ordernum);
2213  
2214         img_buf_phys_addr =
2215 -               (unsigned long) virt_to_phys(image_update_buffer);
2216 +               (unsigned long) virt_to_bus(image_update_buffer);
2217  
2218         if (img_buf_phys_addr > BIOS_SCAN_LIMIT) {
2219                 free_pages((unsigned long) image_update_buffer, ordernum);
2220 +#else
2221 +       {
2222 +#endif
2223                 ordernum = -1;
2224                 image_update_buffer = dma_alloc_coherent(NULL, size,
2225                         &dell_rbu_dmaaddr, GFP_KERNEL);
2226 @@ -695,6 +727,12 @@ static struct bin_attribute rbu_packet_s
2227  static int __init dcdrbu_init(void)
2228  {
2229         int rc;
2230 +
2231 +#ifdef CONFIG_XEN
2232 +       if (!is_initial_xendomain())
2233 +               return -ENODEV;
2234 +#endif
2235 +
2236         spin_lock_init(&rbu_data.lock);
2237  
2238         init_packet_head();
2239 --- head-2011-10-07.orig/drivers/ide/ide-lib.c  2011-10-07 10:46:17.000000000 +0200
2240 +++ head-2011-10-07/drivers/ide/ide-lib.c       2011-04-13 11:25:29.000000000 +0200
2241 @@ -18,12 +18,12 @@ void ide_toggle_bounce(ide_drive_t *driv
2242  {
2243         u64 addr = BLK_BOUNCE_HIGH;     /* dma64_addr_t */
2244  
2245 -       if (!PCI_DMA_BUS_IS_PHYS) {
2246 -               addr = BLK_BOUNCE_ANY;
2247 -       } else if (on && drive->media == ide_disk) {
2248 +       if (on && drive->media == ide_disk) {
2249                 struct device *dev = drive->hwif->dev;
2250  
2251 -               if (dev && dev->dma_mask)
2252 +               if (!PCI_DMA_BUS_IS_PHYS)
2253 +                       addr = BLK_BOUNCE_ANY;
2254 +               else if (dev && dev->dma_mask)
2255                         addr = *dev->dma_mask;
2256         }
2257  
2258 --- head-2011-10-07.orig/drivers/oprofile/buffer_sync.c 2011-10-07 10:46:17.000000000 +0200
2259 +++ head-2011-10-07/drivers/oprofile/buffer_sync.c      2011-06-30 15:37:10.000000000 +0200
2260 @@ -8,6 +8,10 @@
2261   * @author Barry Kasindorf
2262   * @author Robert Richter <robert.richter@amd.com>
2263   *
2264 + * Modified by Aravind Menon for Xen
2265 + * These modifications are:
2266 + * Copyright (C) 2005 Hewlett-Packard Co.
2267 + *
2268   * This is the core of the buffer management. Each
2269   * CPU buffer is processed and entered into the
2270   * global event buffer. Such processing is necessary
2271 @@ -43,6 +47,8 @@ static cpumask_var_t marked_cpus;
2272  static DEFINE_SPINLOCK(task_mortuary);
2273  static void process_task_mortuary(void);
2274  
2275 +static int cpu_current_domain[NR_CPUS];
2276 +
2277  /* Take ownership of the task struct and place it on the
2278   * list for processing. Only after two full buffer syncs
2279   * does the task eventually get freed, because by then
2280 @@ -61,7 +67,6 @@ task_free_notify(struct notifier_block *
2281         return NOTIFY_OK;
2282  }
2283  
2284 -
2285  /* The task is on its way out. A sync of the buffer means we can catch
2286   * any remaining samples for this task.
2287   */
2288 @@ -151,6 +156,11 @@ static void free_all_tasks(void)
2289  int sync_start(void)
2290  {
2291         int err;
2292 +       int i;
2293 +
2294 +       for (i = 0; i < NR_CPUS; i++) {
2295 +               cpu_current_domain[i] = COORDINATOR_DOMAIN;
2296 +       }
2297  
2298         if (!zalloc_cpumask_var(&marked_cpus, GFP_KERNEL))
2299                 return -ENOMEM;
2300 @@ -287,13 +297,29 @@ static void add_cpu_switch(int i)
2301         last_cookie = INVALID_COOKIE;
2302  }
2303  
2304 -static void add_kernel_ctx_switch(unsigned int in_kernel)
2305 +static void add_cpu_mode_switch(unsigned int cpu_mode)
2306  {
2307         add_event_entry(ESCAPE_CODE);
2308 -       if (in_kernel)
2309 +       switch (cpu_mode) {
2310 +       case CPU_MODE_USER:
2311 +               add_event_entry(USER_ENTER_SWITCH_CODE);
2312 +               break;
2313 +       case CPU_MODE_KERNEL:
2314                 add_event_entry(KERNEL_ENTER_SWITCH_CODE);
2315 -       else
2316 -               add_event_entry(KERNEL_EXIT_SWITCH_CODE);
2317 +               break;
2318 +       case CPU_MODE_XEN:
2319 +               add_event_entry(XEN_ENTER_SWITCH_CODE);
2320 +               break;
2321 +       default:
2322 +               break;
2323 +       }
2324 +}
2325 +
2326 +static void add_domain_switch(unsigned long domain_id)
2327 +{
2328 +       add_event_entry(ESCAPE_CODE);
2329 +       add_event_entry(DOMAIN_SWITCH_CODE);
2330 +       add_event_entry(domain_id);
2331  }
2332  
2333  static void
2334 @@ -374,12 +400,12 @@ static inline void add_sample_entry(unsi
2335   * for later lookup from userspace. Return 0 on failure.
2336   */
2337  static int
2338 -add_sample(struct mm_struct *mm, struct op_sample *s, int in_kernel)
2339 +add_sample(struct mm_struct *mm, struct op_sample *s, int cpu_mode)
2340  {
2341         unsigned long cookie;
2342         off_t offset;
2343  
2344 -       if (in_kernel) {
2345 +       if (cpu_mode >= CPU_MODE_KERNEL) {
2346                 add_sample_entry(s->eip, s->event);
2347                 return 1;
2348         }
2349 @@ -504,9 +530,10 @@ void sync_buffer(int cpu)
2350         unsigned long val;
2351         struct task_struct *new;
2352         unsigned long cookie = 0;
2353 -       int in_kernel = 1;
2354 +       int cpu_mode = CPU_MODE_KERNEL;
2355         sync_buffer_state state = sb_buffer_start;
2356         unsigned int i;
2357 +       int domain_switch = 0;
2358         unsigned long available;
2359         unsigned long flags;
2360         struct op_entry entry;
2361 @@ -516,6 +543,11 @@ void sync_buffer(int cpu)
2362  
2363         add_cpu_switch(cpu);
2364  
2365 +       /* We need to assign the first samples in this CPU buffer to the
2366 +          same domain that we were processing at the last sync_buffer */
2367 +       if (cpu_current_domain[cpu] != COORDINATOR_DOMAIN)
2368 +               add_domain_switch(cpu_current_domain[cpu]);
2369 +
2370         op_cpu_buffer_reset(cpu);
2371         available = op_cpu_buffer_entries(cpu);
2372  
2373 @@ -524,6 +556,13 @@ void sync_buffer(int cpu)
2374                 if (!sample)
2375                         break;
2376  
2377 +               if (domain_switch) {
2378 +                       cpu_current_domain[cpu] = sample->eip;
2379 +                       add_domain_switch(sample->eip);
2380 +                       domain_switch = 0;
2381 +                       continue;
2382 +               }
2383 +
2384                 if (is_code(sample->eip)) {
2385                         flags = sample->event;
2386                         if (flags & TRACE_BEGIN) {
2387 @@ -532,10 +571,10 @@ void sync_buffer(int cpu)
2388                         }
2389                         if (flags & KERNEL_CTX_SWITCH) {
2390                                 /* kernel/userspace switch */
2391 -                               in_kernel = flags & IS_KERNEL;
2392 +                               cpu_mode = flags & CPU_MODE_MASK;
2393                                 if (state == sb_buffer_start)
2394                                         state = sb_sample_start;
2395 -                               add_kernel_ctx_switch(flags & IS_KERNEL);
2396 +                               add_cpu_mode_switch(cpu_mode);
2397                         }
2398                         if (flags & USER_CTX_SWITCH
2399                             && op_cpu_buffer_get_data(&entry, &val)) {
2400 @@ -548,16 +587,23 @@ void sync_buffer(int cpu)
2401                                         cookie = get_exec_dcookie(mm);
2402                                 add_user_ctx_switch(new, cookie);
2403                         }
2404 +                       if (flags & DOMAIN_SWITCH)
2405 +                               domain_switch = 1;
2406                         if (op_cpu_buffer_get_size(&entry))
2407                                 add_data(&entry, mm);
2408                         continue;
2409                 }
2410  
2411 +               if (cpu_current_domain[cpu] != COORDINATOR_DOMAIN) {
2412 +                       add_sample_entry(sample->eip, sample->event);
2413 +                       continue;
2414 +               }
2415 +
2416                 if (state < sb_bt_start)
2417                         /* ignore sample */
2418                         continue;
2419  
2420 -               if (add_sample(mm, sample, in_kernel))
2421 +               if (add_sample(mm, sample, cpu_mode))
2422                         continue;
2423  
2424                 /* ignore backtraces if failed to add a sample */
2425 @@ -568,6 +614,10 @@ void sync_buffer(int cpu)
2426         }
2427         release_mm(mm);
2428  
2429 +       /* We reset domain to COORDINATOR at each CPU switch */
2430 +       if (cpu_current_domain[cpu] != COORDINATOR_DOMAIN)
2431 +               add_domain_switch(COORDINATOR_DOMAIN);
2432 +
2433         mark_done(cpu);
2434  
2435         mutex_unlock(&buffer_mutex);
2436 --- head-2011-10-07.orig/drivers/oprofile/cpu_buffer.c  2011-10-07 10:46:17.000000000 +0200
2437 +++ head-2011-10-07/drivers/oprofile/cpu_buffer.c       2011-04-13 11:25:29.000000000 +0200
2438 @@ -8,6 +8,10 @@
2439   * @author Barry Kasindorf <barry.kasindorf@amd.com>
2440   * @author Robert Richter <robert.richter@amd.com>
2441   *
2442 + * Modified by Aravind Menon for Xen
2443 + * These modifications are:
2444 + * Copyright (C) 2005 Hewlett-Packard Co.
2445 + *
2446   * Each CPU has a local buffer that stores PC value/event
2447   * pairs. We also log context switches when we notice them.
2448   * Eventually each CPU's buffer is processed into the global
2449 @@ -38,6 +42,8 @@ static void wq_sync_buffer(struct work_s
2450  #define DEFAULT_TIMER_EXPIRE (HZ / 10)
2451  static int work_enabled;
2452  
2453 +static int32_t current_domain = COORDINATOR_DOMAIN;
2454 +
2455  unsigned long oprofile_get_cpu_buffer_size(void)
2456  {
2457         return oprofile_cpu_buffer_size;
2458 @@ -75,7 +81,7 @@ int alloc_cpu_buffers(void)
2459                 struct oprofile_cpu_buffer *b = &per_cpu(op_cpu_buffer, i);
2460  
2461                 b->last_task = NULL;
2462 -               b->last_is_kernel = -1;
2463 +               b->last_cpu_mode = -1;
2464                 b->tracing = 0;
2465                 b->buffer_size = buffer_size;
2466                 b->sample_received = 0;
2467 @@ -180,7 +186,7 @@ unsigned long op_cpu_buffer_entries(int 
2468  
2469  static int
2470  op_add_code(struct oprofile_cpu_buffer *cpu_buf, unsigned long backtrace,
2471 -           int is_kernel, struct task_struct *task)
2472 +           int cpu_mode, struct task_struct *task)
2473  {
2474         struct op_entry entry;
2475         struct op_sample *sample;
2476 @@ -193,16 +199,15 @@ op_add_code(struct oprofile_cpu_buffer *
2477                 flags |= TRACE_BEGIN;
2478  
2479         /* notice a switch from user->kernel or vice versa */
2480 -       is_kernel = !!is_kernel;
2481 -       if (cpu_buf->last_is_kernel != is_kernel) {
2482 -               cpu_buf->last_is_kernel = is_kernel;
2483 -               flags |= KERNEL_CTX_SWITCH;
2484 -               if (is_kernel)
2485 -                       flags |= IS_KERNEL;
2486 +       if (cpu_buf->last_cpu_mode != cpu_mode) {
2487 +               cpu_buf->last_cpu_mode = cpu_mode;
2488 +               flags |= KERNEL_CTX_SWITCH | cpu_mode;
2489         }
2490  
2491         /* notice a task switch */
2492 -       if (cpu_buf->last_task != task) {
2493 +       /* if not processing other domain samples */
2494 +       if (cpu_buf->last_task != task &&
2495 +           current_domain == COORDINATOR_DOMAIN) {
2496                 cpu_buf->last_task = task;
2497                 flags |= USER_CTX_SWITCH;
2498         }
2499 @@ -251,14 +256,14 @@ op_add_sample(struct oprofile_cpu_buffer
2500  /*
2501   * This must be safe from any context.
2502   *
2503 - * is_kernel is needed because on some architectures you cannot
2504 + * cpu_mode is needed because on some architectures you cannot
2505   * tell if you are in kernel or user space simply by looking at
2506 - * pc. We tag this in the buffer by generating kernel enter/exit
2507 - * events whenever is_kernel changes
2508 + * pc. We tag this in the buffer by generating kernel/user (and
2509 + * xen) enter events whenever cpu_mode changes
2510   */
2511  static int
2512  log_sample(struct oprofile_cpu_buffer *cpu_buf, unsigned long pc,
2513 -          unsigned long backtrace, int is_kernel, unsigned long event,
2514 +          unsigned long backtrace, int cpu_mode, unsigned long event,
2515            struct task_struct *task)
2516  {
2517         struct task_struct *tsk = task ? task : current;
2518 @@ -269,7 +274,7 @@ log_sample(struct oprofile_cpu_buffer *c
2519                 return 0;
2520         }
2521  
2522 -       if (op_add_code(cpu_buf, backtrace, is_kernel, tsk))
2523 +       if (op_add_code(cpu_buf, backtrace, cpu_mode, tsk))
2524                 goto fail;
2525  
2526         if (op_add_sample(cpu_buf, pc, event))
2527 @@ -440,6 +445,25 @@ fail:
2528         return;
2529  }
2530  
2531 +int oprofile_add_domain_switch(int32_t domain_id)
2532 +{
2533 +       struct oprofile_cpu_buffer * cpu_buf = &cpu_buffer[smp_processor_id()];
2534 +
2535 +       /* should have space for switching into and out of domain
2536 +          (2 slots each) plus one sample and one cpu mode switch */
2537 +       if (((nr_available_slots(cpu_buf) < 6) &&
2538 +            (domain_id != COORDINATOR_DOMAIN)) ||
2539 +           (nr_available_slots(cpu_buf) < 2))
2540 +               return 0;
2541 +
2542 +       add_code(cpu_buf, DOMAIN_SWITCH);
2543 +       add_sample(cpu_buf, domain_id, 0);
2544 +
2545 +       current_domain = domain_id;
2546 +
2547 +       return 1;
2548 +}
2549 +
2550  /*
2551   * This serves to avoid cpu buffer overflow, and makes sure
2552   * the task mortuary progresses
2553 --- head-2011-10-07.orig/drivers/oprofile/cpu_buffer.h  2011-10-07 10:46:17.000000000 +0200
2554 +++ head-2011-10-07/drivers/oprofile/cpu_buffer.h       2011-04-13 11:25:29.000000000 +0200
2555 @@ -41,7 +41,7 @@ struct op_entry;
2556  struct oprofile_cpu_buffer {
2557         unsigned long buffer_size;
2558         struct task_struct *last_task;
2559 -       int last_is_kernel;
2560 +       int last_cpu_mode;
2561         int tracing;
2562         unsigned long sample_received;
2563         unsigned long sample_lost_overflow;
2564 @@ -63,7 +63,7 @@ static inline void op_cpu_buffer_reset(i
2565  {
2566         struct oprofile_cpu_buffer *cpu_buf = &per_cpu(op_cpu_buffer, cpu);
2567  
2568 -       cpu_buf->last_is_kernel = -1;
2569 +       cpu_buf->last_cpu_mode = -1;
2570         cpu_buf->last_task = NULL;
2571  }
2572  
2573 @@ -113,9 +113,13 @@ int op_cpu_buffer_get_data(struct op_ent
2574  }
2575  
2576  /* extra data flags */
2577 -#define KERNEL_CTX_SWITCH      (1UL << 0)
2578 -#define IS_KERNEL              (1UL << 1)
2579 +#define CPU_MODE_USER          0
2580 +#define CPU_MODE_KERNEL                1
2581 +#define CPU_MODE_XEN           2
2582 +#define CPU_MODE_MASK          3
2583  #define TRACE_BEGIN            (1UL << 2)
2584  #define USER_CTX_SWITCH                (1UL << 3)
2585 +#define KERNEL_CTX_SWITCH      (1UL << 4)
2586 +#define DOMAIN_SWITCH          (1UL << 5)
2587  
2588  #endif /* OPROFILE_CPU_BUFFER_H */
2589 --- head-2011-10-07.orig/drivers/oprofile/event_buffer.h        2011-10-07 10:46:17.000000000 +0200
2590 +++ head-2011-10-07/drivers/oprofile/event_buffer.h     2011-04-13 11:25:29.000000000 +0200
2591 @@ -30,6 +30,9 @@ void wake_up_buffer_waiter(void);
2592  #define INVALID_COOKIE ~0UL
2593  #define NO_COOKIE 0UL
2594  
2595 +/* Constant used to refer to coordinator domain (Xen) */
2596 +#define COORDINATOR_DOMAIN -1
2597 +
2598  extern const struct file_operations event_buffer_fops;
2599  
2600  /* mutex between sync_cpu_buffers() and the
2601 --- head-2011-10-07.orig/drivers/oprofile/oprof.c       2011-10-07 10:46:17.000000000 +0200
2602 +++ head-2011-10-07/drivers/oprofile/oprof.c    2011-04-13 11:25:29.000000000 +0200
2603 @@ -5,6 +5,10 @@
2604   * @remark Read the file COPYING
2605   *
2606   * @author John Levon <levon@movementarian.org>
2607 + *
2608 + * Modified by Aravind Menon for Xen
2609 + * These modifications are:
2610 + * Copyright (C) 2005 Hewlett-Packard Co.
2611   */
2612  
2613  #include <linux/kernel.h>
2614 @@ -35,6 +39,32 @@ static DEFINE_MUTEX(start_mutex);
2615   */
2616  static int timer = 0;
2617  
2618 +int oprofile_set_active(int active_domains[], unsigned int adomains)
2619 +{
2620 +       int err;
2621 +
2622 +       if (!oprofile_ops.set_active)
2623 +               return -EINVAL;
2624 +
2625 +       mutex_lock(&start_mutex);
2626 +       err = oprofile_ops.set_active(active_domains, adomains);
2627 +       mutex_unlock(&start_mutex);
2628 +       return err;
2629 +}
2630 +
2631 +int oprofile_set_passive(int passive_domains[], unsigned int pdomains)
2632 +{
2633 +       int err;
2634 +
2635 +       if (!oprofile_ops.set_passive)
2636 +               return -EINVAL;
2637 +
2638 +       mutex_lock(&start_mutex);
2639 +       err = oprofile_ops.set_passive(passive_domains, pdomains);
2640 +       mutex_unlock(&start_mutex);
2641 +       return err;
2642 +}
2643 +
2644  int oprofile_setup(void)
2645  {
2646         int err;
2647 --- head-2011-10-07.orig/drivers/oprofile/oprof.h       2011-10-07 10:46:17.000000000 +0200
2648 +++ head-2011-10-07/drivers/oprofile/oprof.h    2011-04-13 11:25:29.000000000 +0200
2649 @@ -40,4 +40,7 @@ void oprofile_timer_exit(void);
2650  int oprofile_set_ulong(unsigned long *addr, unsigned long val);
2651  int oprofile_set_timeout(unsigned long time);
2652  
2653 +int oprofile_set_active(int active_domains[], unsigned int adomains);
2654 +int oprofile_set_passive(int passive_domains[], unsigned int pdomains);
2655 +
2656  #endif /* OPROF_H */
2657 --- head-2011-10-07.orig/drivers/oprofile/oprofile_files.c      2011-10-07 10:46:17.000000000 +0200
2658 +++ head-2011-10-07/drivers/oprofile/oprofile_files.c   2011-04-13 11:25:29.000000000 +0200
2659 @@ -5,11 +5,17 @@
2660   * @remark Read the file COPYING
2661   *
2662   * @author John Levon <levon@movementarian.org>
2663 + *
2664 + * Modified by Aravind Menon for Xen
2665 + * These modifications are:
2666 + * Copyright (C) 2005 Hewlett-Packard Co.
2667   */
2668  
2669  #include <linux/fs.h>
2670  #include <linux/oprofile.h>
2671  #include <linux/jiffies.h>
2672 +#include <asm/uaccess.h>
2673 +#include <linux/ctype.h>
2674  
2675  #include "event_buffer.h"
2676  #include "oprofile_stats.h"
2677 @@ -174,6 +180,195 @@ static const struct file_operations dump
2678         .llseek         = noop_llseek,
2679  };
2680  
2681 +#define TMPBUFSIZE 512
2682 +
2683 +static unsigned int adomains = 0;
2684 +static int active_domains[MAX_OPROF_DOMAINS + 1];
2685 +static DEFINE_MUTEX(adom_mutex);
2686 +
2687 +static ssize_t adomain_write(struct file * file, char const __user * buf,
2688 +                            size_t count, loff_t * offset)
2689 +{
2690 +       char *tmpbuf;
2691 +       char *startp, *endp;
2692 +       int i;
2693 +       unsigned long val;
2694 +       ssize_t retval = count;
2695 +
2696 +       if (*offset)
2697 +               return -EINVAL;
2698 +       if (count > TMPBUFSIZE - 1)
2699 +               return -EINVAL;
2700 +
2701 +       if (!(tmpbuf = kmalloc(TMPBUFSIZE, GFP_KERNEL)))
2702 +               return -ENOMEM;
2703 +
2704 +       if (copy_from_user(tmpbuf, buf, count)) {
2705 +               kfree(tmpbuf);
2706 +               return -EFAULT;
2707 +       }
2708 +       tmpbuf[count] = 0;
2709 +
2710 +       mutex_lock(&adom_mutex);
2711 +
2712 +       startp = tmpbuf;
2713 +       /* Parse one more than MAX_OPROF_DOMAINS, for easy error checking */
2714 +       for (i = 0; i <= MAX_OPROF_DOMAINS; i++) {
2715 +               val = simple_strtoul(startp, &endp, 0);
2716 +               if (endp == startp)
2717 +                       break;
2718 +               while (ispunct(*endp) || isspace(*endp))
2719 +                       endp++;
2720 +               active_domains[i] = val;
2721 +               if (active_domains[i] != val)
2722 +                       /* Overflow, force error below */
2723 +                       i = MAX_OPROF_DOMAINS + 1;
2724 +               startp = endp;
2725 +       }
2726 +       /* Force error on trailing junk */
2727 +       adomains = *startp ? MAX_OPROF_DOMAINS + 1 : i;
2728 +
2729 +       kfree(tmpbuf);
2730 +
2731 +       if (adomains > MAX_OPROF_DOMAINS
2732 +           || oprofile_set_active(active_domains, adomains)) {
2733 +               adomains = 0;
2734 +               retval = -EINVAL;
2735 +       }
2736 +
2737 +       mutex_unlock(&adom_mutex);
2738 +       return retval;
2739 +}
2740 +
2741 +static ssize_t adomain_read(struct file * file, char __user * buf,
2742 +                           size_t count, loff_t * offset)
2743 +{
2744 +       char * tmpbuf;
2745 +       size_t len;
2746 +       int i;
2747 +       ssize_t retval;
2748 +
2749 +       if (!(tmpbuf = kmalloc(TMPBUFSIZE, GFP_KERNEL)))
2750 +               return -ENOMEM;
2751 +
2752 +       mutex_lock(&adom_mutex);
2753 +
2754 +       len = 0;
2755 +       for (i = 0; i < adomains; i++)
2756 +               len += snprintf(tmpbuf + len,
2757 +                               len < TMPBUFSIZE ? TMPBUFSIZE - len : 0,
2758 +                               "%u ", active_domains[i]);
2759 +       WARN_ON(len > TMPBUFSIZE);
2760 +       if (len != 0 && len <= TMPBUFSIZE)
2761 +               tmpbuf[len-1] = '\n';
2762 +
2763 +       mutex_unlock(&adom_mutex);
2764 +
2765 +       retval = simple_read_from_buffer(buf, count, offset, tmpbuf, len);
2766 +
2767 +       kfree(tmpbuf);
2768 +       return retval;
2769 +}
2770 +
2771 +
2772 +static const struct file_operations active_domain_ops = {
2773 +       .read           = adomain_read,
2774 +       .write          = adomain_write,
2775 +};
2776 +
2777 +static unsigned int pdomains = 0;
2778 +static int passive_domains[MAX_OPROF_DOMAINS];
2779 +static DEFINE_MUTEX(pdom_mutex);
2780 +
2781 +static ssize_t pdomain_write(struct file * file, char const __user * buf,
2782 +                            size_t count, loff_t * offset)
2783 +{
2784 +       char *tmpbuf;
2785 +       char *startp, *endp;
2786 +       int i;
2787 +       unsigned long val;
2788 +       ssize_t retval = count;
2789 +
2790 +       if (*offset)
2791 +               return -EINVAL;
2792 +       if (count > TMPBUFSIZE - 1)
2793 +               return -EINVAL;
2794 +
2795 +       if (!(tmpbuf = kmalloc(TMPBUFSIZE, GFP_KERNEL)))
2796 +               return -ENOMEM;
2797 +
2798 +       if (copy_from_user(tmpbuf, buf, count)) {
2799 +               kfree(tmpbuf);
2800 +               return -EFAULT;
2801 +       }
2802 +       tmpbuf[count] = 0;
2803 +
2804 +       mutex_lock(&pdom_mutex);
2805 +
2806 +       startp = tmpbuf;
2807 +       /* Parse one more than MAX_OPROF_DOMAINS, for easy error checking */
2808 +       for (i = 0; i <= MAX_OPROF_DOMAINS; i++) {
2809 +               val = simple_strtoul(startp, &endp, 0);
2810 +               if (endp == startp)
2811 +                       break;
2812 +               while (ispunct(*endp) || isspace(*endp))
2813 +                       endp++;
2814 +               passive_domains[i] = val;
2815 +               if (passive_domains[i] != val)
2816 +                       /* Overflow, force error below */
2817 +                       i = MAX_OPROF_DOMAINS + 1;
2818 +               startp = endp;
2819 +       }
2820 +       /* Force error on trailing junk */
2821 +       pdomains = *startp ? MAX_OPROF_DOMAINS + 1 : i;
2822 +
2823 +       kfree(tmpbuf);
2824 +
2825 +       if (pdomains > MAX_OPROF_DOMAINS
2826 +           || oprofile_set_passive(passive_domains, pdomains)) {
2827 +               pdomains = 0;
2828 +               retval = -EINVAL;
2829 +       }
2830 +
2831 +       mutex_unlock(&pdom_mutex);
2832 +       return retval;
2833 +}
2834 +
2835 +static ssize_t pdomain_read(struct file * file, char __user * buf,
2836 +                           size_t count, loff_t * offset)
2837 +{
2838 +       char * tmpbuf;
2839 +       size_t len;
2840 +       int i;
2841 +       ssize_t retval;
2842 +
2843 +       if (!(tmpbuf = kmalloc(TMPBUFSIZE, GFP_KERNEL)))
2844 +               return -ENOMEM;
2845 +
2846 +       mutex_lock(&pdom_mutex);
2847 +
2848 +       len = 0;
2849 +       for (i = 0; i < pdomains; i++)
2850 +               len += snprintf(tmpbuf + len,
2851 +                               len < TMPBUFSIZE ? TMPBUFSIZE - len : 0,
2852 +                               "%u ", passive_domains[i]);
2853 +       WARN_ON(len > TMPBUFSIZE);
2854 +       if (len != 0 && len <= TMPBUFSIZE)
2855 +               tmpbuf[len-1] = '\n';
2856 +
2857 +       mutex_unlock(&pdom_mutex);
2858 +
2859 +       retval = simple_read_from_buffer(buf, count, offset, tmpbuf, len);
2860 +
2861 +       kfree(tmpbuf);
2862 +       return retval;
2863 +}
2864 +
2865 +static const struct file_operations passive_domain_ops = {
2866 +       .read           = pdomain_read,
2867 +       .write          = pdomain_write,
2868 +};
2869 +
2870  void oprofile_create_files(struct super_block *sb, struct dentry *root)
2871  {
2872         /* reinitialize default values */
2873 @@ -184,6 +379,8 @@ void oprofile_create_files(struct super_
2874  
2875         oprofilefs_create_file(sb, root, "enable", &enable_fops);
2876         oprofilefs_create_file_perm(sb, root, "dump", &dump_fops, 0666);
2877 +       oprofilefs_create_file(sb, root, "active_domains", &active_domain_ops);
2878 +       oprofilefs_create_file(sb, root, "passive_domains", &passive_domain_ops);
2879         oprofilefs_create_file(sb, root, "buffer", &event_buffer_fops);
2880         oprofilefs_create_ulong(sb, root, "buffer_size", &oprofile_buffer_size);
2881         oprofilefs_create_ulong(sb, root, "buffer_watershed", &oprofile_buffer_watershed);
2882 --- head-2011-10-07.orig/fs/aio.c       2011-10-07 10:46:17.000000000 +0200
2883 +++ head-2011-10-07/fs/aio.c    2011-04-13 11:25:29.000000000 +0200
2884 @@ -39,6 +39,11 @@
2885  #include <asm/kmap_types.h>
2886  #include <asm/uaccess.h>
2887  
2888 +#ifdef CONFIG_EPOLL
2889 +#include <linux/poll.h>
2890 +#include <linux/eventpoll.h>
2891 +#endif
2892 +
2893  #if DEBUG > 1
2894  #define dprintk                printk
2895  #else
2896 @@ -991,6 +996,11 @@ put_rq:
2897         if (waitqueue_active(&ctx->wait))
2898                 wake_up(&ctx->wait);
2899  
2900 +#ifdef CONFIG_EPOLL
2901 +       if (ctx->file && waitqueue_active(&ctx->poll_wait))
2902 +               wake_up(&ctx->poll_wait);
2903 +#endif
2904 +
2905         spin_unlock_irqrestore(&ctx->ctx_lock, flags);
2906         return ret;
2907  }
2908 @@ -999,6 +1009,8 @@ EXPORT_SYMBOL(aio_complete);
2909  /* aio_read_evt
2910   *     Pull an event off of the ioctx's event ring.  Returns the number of 
2911   *     events fetched (0 or 1 ;-)
2912 + *     If ent parameter is 0, just returns the number of events that would
2913 + *     be fetched.
2914   *     FIXME: make this use cmpxchg.
2915   *     TODO: make the ringbuffer user mmap()able (requires FIXME).
2916   */
2917 @@ -1021,13 +1033,18 @@ static int aio_read_evt(struct kioctx *i
2918  
2919         head = ring->head % info->nr;
2920         if (head != ring->tail) {
2921 -               struct io_event *evp = aio_ring_event(info, head, KM_USER1);
2922 -               *ent = *evp;
2923 -               head = (head + 1) % info->nr;
2924 -               smp_mb(); /* finish reading the event before updatng the head */
2925 -               ring->head = head;
2926 -               ret = 1;
2927 -               put_aio_ring_event(evp, KM_USER1);
2928 +               if (ent) { /* event requested */
2929 +                       struct io_event *evp =
2930 +                               aio_ring_event(info, head, KM_USER1);
2931 +                       *ent = *evp;
2932 +                       head = (head + 1) % info->nr;
2933 +                       /* finish reading the event before updatng the head */
2934 +                       smp_mb();
2935 +                       ring->head = head;
2936 +                       ret = 1;
2937 +                       put_aio_ring_event(evp, KM_USER1);
2938 +               } else /* only need to know availability */
2939 +                       ret = 1;
2940         }
2941         spin_unlock(&info->ring_lock);
2942  
2943 @@ -1212,6 +1229,13 @@ static void io_destroy(struct kioctx *io
2944  
2945         aio_cancel_all(ioctx);
2946         wait_for_all_aios(ioctx);
2947 +#ifdef CONFIG_EPOLL
2948 +       /* forget the poll file, but it's up to the user to close it */
2949 +       if (ioctx->file) {
2950 +               ioctx->file->private_data = 0;
2951 +               ioctx->file = 0;
2952 +       }
2953 +#endif
2954  
2955         /*
2956          * Wake up any waiters.  The setting of ctx->dead must be seen
2957 @@ -1222,6 +1246,67 @@ static void io_destroy(struct kioctx *io
2958         put_ioctx(ioctx);       /* once for the lookup */
2959  }
2960  
2961 +#ifdef CONFIG_EPOLL
2962 +
2963 +static int aio_queue_fd_close(struct inode *inode, struct file *file)
2964 +{
2965 +       struct kioctx *ioctx = file->private_data;
2966 +       if (ioctx) {
2967 +               file->private_data = 0;
2968 +               spin_lock_irq(&ioctx->ctx_lock);
2969 +               ioctx->file = 0;
2970 +               spin_unlock_irq(&ioctx->ctx_lock);
2971 +       }
2972 +       return 0;
2973 +}
2974 +
2975 +static unsigned int aio_queue_fd_poll(struct file *file, poll_table *wait)
2976 +{      unsigned int pollflags = 0;
2977 +       struct kioctx *ioctx = file->private_data;
2978 +
2979 +       if (ioctx) {
2980 +
2981 +               spin_lock_irq(&ioctx->ctx_lock);
2982 +               /* Insert inside our poll wait queue */
2983 +               poll_wait(file, &ioctx->poll_wait, wait);
2984 +
2985 +               /* Check our condition */
2986 +               if (aio_read_evt(ioctx, 0))
2987 +                       pollflags = POLLIN | POLLRDNORM;
2988 +               spin_unlock_irq(&ioctx->ctx_lock);
2989 +       }
2990 +
2991 +       return pollflags;
2992 +}
2993 +
2994 +static const struct file_operations aioq_fops = {
2995 +       .release        = aio_queue_fd_close,
2996 +       .poll           = aio_queue_fd_poll
2997 +};
2998 +
2999 +/* make_aio_fd:
3000 + *  Create a file descriptor that can be used to poll the event queue.
3001 + *  Based and piggybacked on the excellent epoll code.
3002 + */
3003 +
3004 +static int make_aio_fd(struct kioctx *ioctx)
3005 +{
3006 +       int error, fd;
3007 +       struct inode *inode;
3008 +       struct file *file;
3009 +
3010 +       error = ep_getfd(&fd, &inode, &file, NULL, &aioq_fops);
3011 +       if (error)
3012 +               return error;
3013 +
3014 +       /* associate the file with the IO context */
3015 +       file->private_data = ioctx;
3016 +       ioctx->file = file;
3017 +       init_waitqueue_head(&ioctx->poll_wait);
3018 +       return fd;
3019 +}
3020 +#endif
3021 +
3022  /* sys_io_setup:
3023   *     Create an aio_context capable of receiving at least nr_events.
3024   *     ctxp must not point to an aio_context that already exists, and
3025 @@ -1234,18 +1319,30 @@ static void io_destroy(struct kioctx *io
3026   *     resources are available.  May fail with -EFAULT if an invalid
3027   *     pointer is passed for ctxp.  Will fail with -ENOSYS if not
3028   *     implemented.
3029 + *
3030 + *     To request a selectable fd, the user context has to be initialized
3031 + *     to 1, instead of 0, and the return value is the fd.
3032 + *     This keeps the system call compatible, since a non-zero value
3033 + *     was not allowed so far.
3034   */
3035  SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
3036  {
3037         struct kioctx *ioctx = NULL;
3038         unsigned long ctx;
3039         long ret;
3040 +       int make_fd = 0;
3041  
3042         ret = get_user(ctx, ctxp);
3043         if (unlikely(ret))
3044                 goto out;
3045  
3046         ret = -EINVAL;
3047 +#ifdef CONFIG_EPOLL
3048 +       if (ctx == 1) {
3049 +               make_fd = 1;
3050 +               ctx = 0;
3051 +       }
3052 +#endif
3053         if (unlikely(ctx || nr_events == 0)) {
3054                 pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
3055                          ctx, nr_events);
3056 @@ -1256,8 +1353,12 @@ SYSCALL_DEFINE2(io_setup, unsigned, nr_e
3057         ret = PTR_ERR(ioctx);
3058         if (!IS_ERR(ioctx)) {