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 1116:40e33b2fce94)
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-2011-10-07.orig/drivers/char/agp/intel-gtt.c   2011-10-07 10:46:17.000000000 +0200
648 +++ head-2011-10-07/drivers/char/agp/intel-gtt.c        2011-04-13 11:25:29.000000000 +0200
649 @@ -147,8 +147,19 @@ static struct page *i8xx_alloc_pages(voi
650         if (page == NULL)
651                 return NULL;
652  
653 +#ifdef CONFIG_XEN
654 +       if (xen_create_contiguous_region((unsigned long)page_address(page), 2, 32)) {
655 +               __free_pages(page, 2);
656 +               return NULL;
657 +       }
658 +#endif
659 +
660         if (set_pages_uc(page, 4) < 0) {
661                 set_pages_wb(page, 4);
662 +#ifdef CONFIG_XEN
663 +               xen_destroy_contiguous_region((unsigned long)page_address(page),
664 +                                             2);
665 +#endif
666                 __free_pages(page, 2);
667                 return NULL;
668         }
669 @@ -163,6 +174,9 @@ static void i8xx_destroy_pages(struct pa
670                 return;
671  
672         set_pages_wb(page, 4);
673 +#ifdef CONFIG_XEN
674 +       xen_destroy_contiguous_region((unsigned long)page_address(page), 2);
675 +#endif
676         put_page(page);
677         __free_pages(page, 2);
678         atomic_dec(&agp_bridge->current_memory_agp);
679 --- head-2011-10-07.orig/drivers/char/mem.c     2011-10-07 10:46:17.000000000 +0200
680 +++ head-2011-10-07/drivers/char/mem.c  2011-05-23 10:56:58.000000000 +0200
681 @@ -86,6 +86,7 @@ void __weak unxlate_dev_mem_ptr(unsigned
682  {
683  }
684  
685 +#ifndef ARCH_HAS_DEV_MEM
686  /*
687   * This funcion reads the *physical* memory. The f_pos points directly to the
688   * memory location.
689 @@ -208,6 +209,7 @@ static ssize_t write_mem(struct file *fi
690         *ppos += written;
691         return written;
692  }
693 +#endif
694  
695  int __weak phys_mem_access_prot_allowed(struct file *file,
696         unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
697 @@ -334,6 +336,9 @@ static int mmap_mem(struct file *file, s
698  static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
699  {
700         unsigned long pfn;
701 +#ifdef CONFIG_XEN
702 +       unsigned long i, count;
703 +#endif
704  
705         /* Turn a kernel-virtual address into a physical page frame */
706         pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
707 @@ -348,6 +353,13 @@ static int mmap_kmem(struct file *file, 
708         if (!pfn_valid(pfn))
709                 return -EIO;
710  
711 +#ifdef CONFIG_XEN
712 +       count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
713 +       for (i = 0; i < count; i++)
714 +               if ((pfn + i) != mfn_to_local_pfn(pfn_to_mfn(pfn + i)))
715 +                       return -EIO;
716 +#endif
717 +
718         vma->vm_pgoff = pfn;
719         return mmap_mem(file, vma);
720  }
721 @@ -739,6 +751,7 @@ static int open_port(struct inode * inod
722  #define open_kmem      open_mem
723  #define open_oldmem    open_mem
724  
725 +#ifndef ARCH_HAS_DEV_MEM
726  static const struct file_operations mem_fops = {
727         .llseek         = memory_lseek,
728         .read           = read_mem,
729 @@ -747,6 +760,9 @@ static const struct file_operations mem_
730         .open           = open_mem,
731         .get_unmapped_area = get_unmapped_area_mem,
732  };
733 +#else
734 +extern const struct file_operations mem_fops;
735 +#endif
736  
737  #ifdef CONFIG_DEVKMEM
738  static const struct file_operations kmem_fops = {
739 --- head-2011-10-07.orig/drivers/char/tpm/Makefile      2011-10-07 10:46:17.000000000 +0200
740 +++ head-2011-10-07/drivers/char/tpm/Makefile   2011-04-13 11:25:29.000000000 +0200
741 @@ -9,3 +9,5 @@ obj-$(CONFIG_TCG_TIS) += tpm_tis.o
742  obj-$(CONFIG_TCG_NSC) += tpm_nsc.o
743  obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
744  obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
745 +obj-$(CONFIG_TCG_XEN) += tpm_xenu.o
746 +tpm_xenu-y = tpm_xen.o tpm_vtpm.o
747 --- head-2011-10-07.orig/drivers/char/tpm/tpm.h 2011-10-07 10:46:17.000000000 +0200
748 +++ head-2011-10-07/drivers/char/tpm/tpm.h      2011-09-07 14:38:52.000000000 +0200
749 @@ -115,6 +115,9 @@ struct tpm_chip {
750         struct dentry **bios_dir;
751  
752         struct list_head list;
753 +#ifdef CONFIG_XEN
754 +       void *priv;
755 +#endif
756         void (*release) (struct device *);
757  };
758  
759 @@ -279,6 +282,18 @@ struct tpm_cmd_t {
760  
761  ssize_t        tpm_getcap(struct device *, __be32, cap_t *, const char *);
762  
763 +#ifdef CONFIG_XEN
764 +static inline void *chip_get_private(const struct tpm_chip *chip)
765 +{
766 +       return chip->priv;
767 +}
768 +
769 +static inline void chip_set_private(struct tpm_chip *chip, void *priv)
770 +{
771 +       chip->priv = priv;
772 +}
773 +#endif
774 +
775  extern void tpm_get_timeouts(struct tpm_chip *);
776  extern void tpm_gen_interrupt(struct tpm_chip *);
777  extern void tpm_continue_selftest(struct tpm_chip *);
778 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
779 +++ head-2011-10-07/drivers/char/tpm/tpm_vtpm.c 2011-01-31 14:53:38.000000000 +0100
780 @@ -0,0 +1,542 @@
781 +/*
782 + * Copyright (C) 2006 IBM Corporation
783 + *
784 + * Authors:
785 + * Stefan Berger <stefanb@us.ibm.com>
786 + *
787 + * Generic device driver part for device drivers in a virtualized
788 + * environment.
789 + *
790 + * This program is free software; you can redistribute it and/or
791 + * modify it under the terms of the GNU General Public License as
792 + * published by the Free Software Foundation, version 2 of the
793 + * License.
794 + *
795 + */
796 +
797 +#include <asm/uaccess.h>
798 +#include <linux/list.h>
799 +#include <linux/device.h>
800 +#include <linux/interrupt.h>
801 +#include <linux/platform_device.h>
802 +#include "tpm.h"
803 +#include "tpm_vtpm.h"
804 +
805 +/* read status bits */
806 +enum {
807 +       STATUS_BUSY = 0x01,
808 +       STATUS_DATA_AVAIL = 0x02,
809 +       STATUS_READY = 0x04
810 +};
811 +
812 +struct transmission {
813 +       struct list_head next;
814 +
815 +       unsigned char *request;
816 +       size_t  request_len;
817 +       size_t  request_buflen;
818 +
819 +       unsigned char *response;
820 +       size_t  response_len;
821 +       size_t  response_buflen;
822 +
823 +       unsigned int flags;
824 +};
825 +
826 +enum {
827 +       TRANSMISSION_FLAG_WAS_QUEUED = 0x1
828 +};
829 +
830 +
831 +enum {
832 +       DATAEX_FLAG_QUEUED_ONLY = 0x1
833 +};
834 +
835 +
836 +/* local variables */
837 +
838 +/* local function prototypes */
839 +static int _vtpm_send_queued(struct tpm_chip *chip);
840 +
841 +
842 +/* =============================================================
843 + * Some utility functions
844 + * =============================================================
845 + */
846 +static void vtpm_state_init(struct vtpm_state *vtpms)
847 +{
848 +       vtpms->current_request = NULL;
849 +       spin_lock_init(&vtpms->req_list_lock);
850 +       init_waitqueue_head(&vtpms->req_wait_queue);
851 +       INIT_LIST_HEAD(&vtpms->queued_requests);
852 +
853 +       vtpms->current_response = NULL;
854 +       spin_lock_init(&vtpms->resp_list_lock);
855 +       init_waitqueue_head(&vtpms->resp_wait_queue);
856 +
857 +       vtpms->disconnect_time = jiffies;
858 +}
859 +
860 +
861 +static inline struct transmission *transmission_alloc(void)
862 +{
863 +       return kzalloc(sizeof(struct transmission), GFP_ATOMIC);
864 +}
865 +
866 +static unsigned char *
867 +transmission_set_req_buffer(struct transmission *t,
868 +                            unsigned char *buffer, size_t len)
869 +{
870 +       if (t->request_buflen < len) {
871 +               kfree(t->request);
872 +               t->request = kmalloc(len, GFP_KERNEL);
873 +               if (!t->request) {
874 +                       t->request_buflen = 0;
875 +                       return NULL;
876 +               }
877 +               t->request_buflen = len;
878 +       }
879 +
880 +       memcpy(t->request, buffer, len);
881 +       t->request_len = len;
882 +
883 +       return t->request;
884 +}
885 +
886 +static unsigned char *
887 +transmission_set_res_buffer(struct transmission *t,
888 +                            const unsigned char *buffer, size_t len)
889 +{
890 +       if (t->response_buflen < len) {
891 +               kfree(t->response);
892 +               t->response = kmalloc(len, GFP_ATOMIC);
893 +               if (!t->response) {
894 +                       t->response_buflen = 0;
895 +                       return NULL;
896 +               }
897 +               t->response_buflen = len;
898 +       }
899 +
900 +       memcpy(t->response, buffer, len);
901 +       t->response_len = len;
902 +
903 +       return t->response;
904 +}
905 +
906 +static inline void transmission_free(struct transmission *t)
907 +{
908 +       kfree(t->request);
909 +       kfree(t->response);
910 +       kfree(t);
911 +}
912 +
913 +/* =============================================================
914 + * Interface with the lower layer driver
915 + * =============================================================
916 + */
917 +/*
918 + * Lower layer uses this function to make a response available.
919 + */
920 +int vtpm_vd_recv(const struct tpm_chip *chip,
921 +                 const unsigned char *buffer, size_t count,
922 +                 void *ptr)
923 +{
924 +       unsigned long flags;
925 +       int ret_size = 0;
926 +       struct transmission *t;
927 +       struct vtpm_state *vtpms;
928 +
929 +       vtpms = (struct vtpm_state *)chip_get_private(chip);
930 +
931 +       /*
932 +        * The list with requests must contain one request
933 +        * only and the element there must be the one that
934 +        * was passed to me from the front-end.
935 +        */
936 +       spin_lock_irqsave(&vtpms->resp_list_lock, flags);
937 +       if (vtpms->current_request != ptr) {
938 +               spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
939 +               return 0;
940 +       }
941 +
942 +       if ((t = vtpms->current_request)) {
943 +               transmission_free(t);
944 +               vtpms->current_request = NULL;
945 +       }
946 +
947 +       t = transmission_alloc();
948 +       if (t) {
949 +               if (!transmission_set_res_buffer(t, buffer, count)) {
950 +                       transmission_free(t);
951 +                       spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
952 +                       return -ENOMEM;
953 +               }
954 +               ret_size = count;
955 +               vtpms->current_response = t;
956 +               wake_up_interruptible(&vtpms->resp_wait_queue);
957 +       }
958 +       spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
959 +
960 +       return ret_size;
961 +}
962 +
963 +
964 +/*
965 + * Lower layer indicates its status (connected/disconnected)
966 + */
967 +void vtpm_vd_status(const struct tpm_chip *chip, u8 vd_status)
968 +{
969 +       struct vtpm_state *vtpms;
970 +
971 +       vtpms = (struct vtpm_state *)chip_get_private(chip);
972 +
973 +       vtpms->vd_status = vd_status;
974 +       if ((vtpms->vd_status & TPM_VD_STATUS_CONNECTED) == 0) {
975 +               vtpms->disconnect_time = jiffies;
976 +       }
977 +}
978 +
979 +/* =============================================================
980 + * Interface with the generic TPM driver
981 + * =============================================================
982 + */
983 +static int vtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count)
984 +{
985 +       int rc = 0;
986 +       unsigned long flags;
987 +       struct vtpm_state *vtpms;
988 +
989 +       vtpms = (struct vtpm_state *)chip_get_private(chip);
990 +
991 +       /*
992 +        * Check if the previous operation only queued the command
993 +        * In this case there won't be a response, so I just
994 +        * return from here and reset that flag. In any other
995 +        * case I should receive a response from the back-end.
996 +        */
997 +       spin_lock_irqsave(&vtpms->resp_list_lock, flags);
998 +       if ((vtpms->flags & DATAEX_FLAG_QUEUED_ONLY) != 0) {
999 +               vtpms->flags &= ~DATAEX_FLAG_QUEUED_ONLY;
1000 +               spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
1001 +               /*
1002 +                * The first few commands (measurements) must be
1003 +                * queued since it might not be possible to talk to the
1004 +                * TPM, yet.
1005 +                * Return a response of up to 30 '0's.
1006 +                */
1007 +
1008 +               count = min_t(size_t, count, 30);
1009 +               memset(buf, 0x0, count);
1010 +               return count;
1011 +       }
1012 +       /*
1013 +        * Check whether something is in the responselist and if
1014 +        * there's nothing in the list wait for something to appear.
1015 +        */
1016 +
1017 +       if (!vtpms->current_response) {
1018 +               spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
1019 +               interruptible_sleep_on_timeout(&vtpms->resp_wait_queue,
1020 +                                              1000);
1021 +               spin_lock_irqsave(&vtpms->resp_list_lock ,flags);
1022 +       }
1023 +
1024 +       if (vtpms->current_response) {
1025 +               struct transmission *t = vtpms->current_response;
1026 +               vtpms->current_response = NULL;
1027 +               rc = min(count, t->response_len);
1028 +               memcpy(buf, t->response, rc);
1029 +               transmission_free(t);
1030 +       }
1031 +
1032 +       spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
1033 +       return rc;
1034 +}
1035 +
1036 +static int vtpm_send(struct tpm_chip *chip, u8 *buf, size_t count)
1037 +{
1038 +       int rc = 0;
1039 +       unsigned long flags;
1040 +       struct transmission *t = transmission_alloc();
1041 +       struct vtpm_state *vtpms;
1042 +
1043 +       vtpms = (struct vtpm_state *)chip_get_private(chip);
1044 +
1045 +       if (!t)
1046 +               return -ENOMEM;
1047 +       /*
1048 +        * If there's a current request, it must be the
1049 +        * previous request that has timed out.
1050 +        */
1051 +       spin_lock_irqsave(&vtpms->req_list_lock, flags);
1052 +       if (vtpms->current_request != NULL) {
1053 +               printk("WARNING: Sending although there is a request outstanding.\n"
1054 +                      "         Previous request must have timed out.\n");
1055 +               transmission_free(vtpms->current_request);
1056 +               vtpms->current_request = NULL;
1057 +       }
1058 +       spin_unlock_irqrestore(&vtpms->req_list_lock, flags);
1059 +
1060 +       /*
1061 +        * Queue the packet if the driver below is not
1062 +        * ready, yet, or there is any packet already
1063 +        * in the queue.
1064 +        * If the driver below is ready, unqueue all
1065 +        * packets first before sending our current
1066 +        * packet.
1067 +        * For each unqueued packet, except for the
1068 +        * last (=current) packet, call the function
1069 +        * tpm_xen_recv to wait for the response to come
1070 +        * back.
1071 +        */
1072 +       if ((vtpms->vd_status & TPM_VD_STATUS_CONNECTED) == 0) {
1073 +               if (time_after(jiffies,
1074 +                              vtpms->disconnect_time + HZ * 10)) {
1075 +                       rc = -ENOENT;
1076 +               } else {
1077 +                       goto queue_it;
1078 +               }
1079 +       } else {
1080 +               /*
1081 +                * Send all queued packets.
1082 +                */
1083 +               if (_vtpm_send_queued(chip) == 0) {
1084 +
1085 +                       vtpms->current_request = t;
1086 +
1087 +                       rc = vtpm_vd_send(vtpms->tpm_private,
1088 +                                         buf,
1089 +                                         count,
1090 +                                         t);
1091 +                       /*
1092 +                        * The generic TPM driver will call
1093 +                        * the function to receive the response.
1094 +                        */
1095 +                       if (rc < 0) {
1096 +                               vtpms->current_request = NULL;
1097 +                               goto queue_it;
1098 +                       }
1099 +               } else {
1100 +queue_it:
1101 +                       if (!transmission_set_req_buffer(t, buf, count)) {
1102 +                               transmission_free(t);
1103 +                               rc = -ENOMEM;
1104 +                               goto exit;
1105 +                       }
1106 +                       /*
1107 +                        * An error occurred. Don't event try
1108 +                        * to send the current request. Just
1109 +                        * queue it.
1110 +                        */
1111 +                       spin_lock_irqsave(&vtpms->req_list_lock, flags);
1112 +                       vtpms->flags |= DATAEX_FLAG_QUEUED_ONLY;
1113 +                       list_add_tail(&t->next, &vtpms->queued_requests);
1114 +                       spin_unlock_irqrestore(&vtpms->req_list_lock, flags);
1115 +               }
1116 +       }
1117 +
1118 +exit:
1119 +       return rc;
1120 +}
1121 +
1122 +
1123 +/*
1124 + * Send all queued requests.
1125 + */
1126 +static int _vtpm_send_queued(struct tpm_chip *chip)
1127 +{
1128 +       int rc;
1129 +       int error = 0;
1130 +       long flags;
1131 +       unsigned char buffer[1];
1132 +       struct vtpm_state *vtpms;
1133 +       vtpms = (struct vtpm_state *)chip_get_private(chip);
1134 +
1135 +       spin_lock_irqsave(&vtpms->req_list_lock, flags);
1136 +
1137 +       while (!list_empty(&vtpms->queued_requests)) {
1138 +               /*
1139 +                * Need to dequeue them.
1140 +                * Read the result into a dummy buffer.
1141 +                */
1142 +               struct transmission *qt = (struct transmission *)
1143 +                                         vtpms->queued_requests.next;
1144 +               list_del(&qt->next);
1145 +               vtpms->current_request = qt;
1146 +               spin_unlock_irqrestore(&vtpms->req_list_lock, flags);
1147 +
1148 +               rc = vtpm_vd_send(vtpms->tpm_private,
1149 +                                 qt->request,
1150 +                                 qt->request_len,
1151 +                                 qt);
1152 +
1153 +               if (rc < 0) {
1154 +                       spin_lock_irqsave(&vtpms->req_list_lock, flags);
1155 +                       if ((qt = vtpms->current_request) != NULL) {
1156 +                               /*
1157 +                                * requeue it at the beginning
1158 +                                * of the list
1159 +                                */
1160 +                               list_add(&qt->next,
1161 +                                        &vtpms->queued_requests);
1162 +                       }
1163 +                       vtpms->current_request = NULL;
1164 +                       error = 1;
1165 +                       break;
1166 +               }
1167 +               /*
1168 +                * After this point qt is not valid anymore!
1169 +                * It is freed when the front-end is delivering
1170 +                * the data by calling tpm_recv
1171 +                */
1172 +               /*
1173 +                * Receive response into provided dummy buffer
1174 +                */
1175 +               rc = vtpm_recv(chip, buffer, sizeof(buffer));
1176 +               spin_lock_irqsave(&vtpms->req_list_lock, flags);
1177 +       }
1178 +
1179 +       spin_unlock_irqrestore(&vtpms->req_list_lock, flags);
1180 +
1181 +       return error;
1182 +}
1183 +
1184 +static void vtpm_cancel(struct tpm_chip *chip)
1185 +{
1186 +       unsigned long flags;
1187 +       struct vtpm_state *vtpms = (struct vtpm_state *)chip_get_private(chip);
1188 +
1189 +       spin_lock_irqsave(&vtpms->resp_list_lock,flags);
1190 +
1191 +       if (!vtpms->current_response && vtpms->current_request) {
1192 +               spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
1193 +               interruptible_sleep_on(&vtpms->resp_wait_queue);
1194 +               spin_lock_irqsave(&vtpms->resp_list_lock,flags);
1195 +       }
1196 +
1197 +       if (vtpms->current_response) {
1198 +               struct transmission *t = vtpms->current_response;
1199 +               vtpms->current_response = NULL;
1200 +               transmission_free(t);
1201 +       }
1202 +
1203 +       spin_unlock_irqrestore(&vtpms->resp_list_lock,flags);
1204 +}
1205 +
1206 +static u8 vtpm_status(struct tpm_chip *chip)
1207 +{
1208 +       u8 rc = 0;
1209 +       unsigned long flags;
1210 +       struct vtpm_state *vtpms;
1211 +
1212 +       vtpms = (struct vtpm_state *)chip_get_private(chip);
1213 +
1214 +       spin_lock_irqsave(&vtpms->resp_list_lock, flags);
1215 +       /*
1216 +        * Data are available if:
1217 +        *  - there's a current response
1218 +        *  - the last packet was queued only (this is fake, but necessary to
1219 +        *      get the generic TPM layer to call the receive function.)
1220 +        */
1221 +       if (vtpms->current_response ||
1222 +           0 != (vtpms->flags & DATAEX_FLAG_QUEUED_ONLY)) {
1223 +               rc = STATUS_DATA_AVAIL;
1224 +       } else if (!vtpms->current_response && !vtpms->current_request) {
1225 +               rc = STATUS_READY;
1226 +       }
1227 +
1228 +       spin_unlock_irqrestore(&vtpms->resp_list_lock, flags);
1229 +       return rc;
1230 +}
1231 +
1232 +static struct file_operations vtpm_ops = {
1233 +       .owner = THIS_MODULE,
1234 +       .llseek = no_llseek,
1235 +       .open = tpm_open,
1236 +       .read = tpm_read,
1237 +       .write = tpm_write,
1238 +       .release = tpm_release,
1239 +};
1240 +
1241 +static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
1242 +static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
1243 +static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
1244 +static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
1245 +static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
1246 +static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,
1247 +                  NULL);
1248 +static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
1249 +static DEVICE_ATTR(cancel, S_IWUSR |S_IWGRP, NULL, tpm_store_cancel);
1250 +
1251 +static struct attribute *vtpm_attrs[] = {
1252 +       &dev_attr_pubek.attr,
1253 +       &dev_attr_pcrs.attr,
1254 +       &dev_attr_enabled.attr,
1255 +       &dev_attr_active.attr,
1256 +       &dev_attr_owned.attr,
1257 +       &dev_attr_temp_deactivated.attr,
1258 +       &dev_attr_caps.attr,
1259 +       &dev_attr_cancel.attr,
1260 +       NULL,
1261 +};
1262 +
1263 +static struct attribute_group vtpm_attr_grp = { .attrs = vtpm_attrs };
1264 +
1265 +#define TPM_LONG_TIMEOUT   (10 * 60 * HZ)
1266 +
1267 +static struct tpm_vendor_specific tpm_vtpm = {
1268 +       .recv = vtpm_recv,
1269 +       .send = vtpm_send,
1270 +       .cancel = vtpm_cancel,
1271 +       .status = vtpm_status,
1272 +       .req_complete_mask = STATUS_BUSY | STATUS_DATA_AVAIL,
1273 +       .req_complete_val  = STATUS_DATA_AVAIL,
1274 +       .req_canceled = STATUS_READY,
1275 +       .attr_group = &vtpm_attr_grp,
1276 +       .miscdev = {
1277 +               .fops = &vtpm_ops,
1278 +       },
1279 +       .duration = {
1280 +               TPM_LONG_TIMEOUT,
1281 +               TPM_LONG_TIMEOUT,
1282 +               TPM_LONG_TIMEOUT,
1283 +       },
1284 +};
1285 +
1286 +struct tpm_chip *init_vtpm(struct device *dev,
1287 +                           struct tpm_private *tp)
1288 +{
1289 +       long rc;
1290 +       struct tpm_chip *chip;
1291 +       struct vtpm_state *vtpms;
1292 +
1293 +       vtpms = kzalloc(sizeof(struct vtpm_state), GFP_KERNEL);
1294 +       if (!vtpms)
1295 +               return ERR_PTR(-ENOMEM);
1296 +
1297 +       vtpm_state_init(vtpms);
1298 +       vtpms->tpm_private = tp;
1299 +
1300 +       chip = tpm_register_hardware(dev, &tpm_vtpm);
1301 +       if (!chip) {
1302 +               rc = -ENODEV;
1303 +               goto err_free_mem;
1304 +       }
1305 +
1306 +       chip_set_private(chip, vtpms);
1307 +
1308 +       return chip;
1309 +
1310 +err_free_mem:
1311 +       kfree(vtpms);
1312 +
1313 +       return ERR_PTR(rc);
1314 +}
1315 +
1316 +void cleanup_vtpm(struct device *dev)
1317 +{
1318 +       struct tpm_chip *chip = dev_get_drvdata(dev);
1319 +       struct vtpm_state *vtpms = (struct vtpm_state*)chip_get_private(chip);
1320 +       tpm_remove_hardware(dev);
1321 +       kfree(vtpms);
1322 +}
1323 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
1324 +++ head-2011-10-07/drivers/char/tpm/tpm_vtpm.h 2011-01-31 14:53:38.000000000 +0100
1325 @@ -0,0 +1,55 @@
1326 +#ifndef TPM_VTPM_H
1327 +#define TPM_VTPM_H
1328 +
1329 +struct tpm_chip;
1330 +struct tpm_private;
1331 +
1332 +struct vtpm_state {
1333 +       struct transmission *current_request;
1334 +       spinlock_t           req_list_lock;
1335 +       wait_queue_head_t    req_wait_queue;
1336 +
1337 +       struct list_head     queued_requests;
1338 +
1339 +       struct transmission *current_response;
1340 +       spinlock_t           resp_list_lock;
1341 +       wait_queue_head_t    resp_wait_queue;     // processes waiting for responses
1342 +
1343 +       u8                   vd_status;
1344 +       u8                   flags;
1345 +
1346 +       unsigned long        disconnect_time;
1347 +
1348 +       /*
1349 +        * The following is a private structure of the underlying
1350 +        * driver. It is passed as parameter in the send function.
1351 +        */
1352 +       struct tpm_private *tpm_private;
1353 +};
1354 +
1355 +
1356 +enum vdev_status {
1357 +       TPM_VD_STATUS_DISCONNECTED = 0x0,
1358 +       TPM_VD_STATUS_CONNECTED = 0x1
1359 +};
1360 +
1361 +/* this function is called from tpm_vtpm.c */
1362 +int vtpm_vd_send(struct tpm_private * tp,
1363 +                 const u8 * buf, size_t count, void *ptr);
1364 +
1365 +/* these functions are offered by tpm_vtpm.c */
1366 +struct tpm_chip *init_vtpm(struct device *,
1367 +                           struct tpm_private *);
1368 +void cleanup_vtpm(struct device *);
1369 +int vtpm_vd_recv(const struct tpm_chip* chip,
1370 +                 const unsigned char *buffer, size_t count, void *ptr);
1371 +void vtpm_vd_status(const struct tpm_chip *, u8 status);
1372 +
1373 +static inline struct tpm_private *tpm_private_from_dev(struct device *dev)
1374 +{
1375 +       struct tpm_chip *chip = dev_get_drvdata(dev);
1376 +       struct vtpm_state *vtpms = chip_get_private(chip);
1377 +       return vtpms->tpm_private;
1378 +}
1379 +
1380 +#endif
1381 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
1382 +++ head-2011-10-07/drivers/char/tpm/tpm_xen.c  2011-01-31 14:53:38.000000000 +0100
1383 @@ -0,0 +1,722 @@
1384 +/*
1385 + * Copyright (c) 2005, IBM Corporation
1386 + *
1387 + * Author: Stefan Berger, stefanb@us.ibm.com
1388 + * Grant table support: Mahadevan Gomathisankaran
1389 + *
1390 + * This code has been derived from drivers/xen/netfront/netfront.c
1391 + *
1392 + * Copyright (c) 2002-2004, K A Fraser
1393 + *
1394 + * This program is free software; you can redistribute it and/or
1395 + * modify it under the terms of the GNU General Public License version 2
1396 + * as published by the Free Software Foundation; or, when distributed
1397 + * separately from the Linux kernel or incorporated into other
1398 + * software packages, subject to the following license:
1399 + *
1400 + * Permission is hereby granted, free of charge, to any person obtaining a copy
1401 + * of this source file (the "Software"), to deal in the Software without
1402 + * restriction, including without limitation the rights to use, copy, modify,
1403 + * merge, publish, distribute, sublicense, and/or sell copies of the Software,
1404 + * and to permit persons to whom the Software is furnished to do so, subject to
1405 + * the following conditions:
1406 + *
1407 + * The above copyright notice and this permission notice shall be included in
1408 + * all copies or substantial portions of the Software.
1409 + *
1410 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1411 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1412 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1413 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1414 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1415 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
1416 + * IN THE SOFTWARE.
1417 + */
1418 +
1419 +#include <linux/errno.h>
1420 +#include <linux/err.h>
1421 +#include <linux/interrupt.h>
1422 +#include <linux/mutex.h>
1423 +#include <asm/uaccess.h>
1424 +#include <xen/evtchn.h>
1425 +#include <xen/interface/grant_table.h>
1426 +#include <xen/interface/io/tpmif.h>
1427 +#include <xen/gnttab.h>
1428 +#include <xen/xenbus.h>
1429 +#include "tpm.h"
1430 +#include "tpm_vtpm.h"
1431 +
1432 +#undef DEBUG
1433 +
1434 +/* local structures */
1435 +struct tpm_private {
1436 +       struct tpm_chip *chip;
1437 +
1438 +       tpmif_tx_interface_t *tx;
1439 +       atomic_t refcnt;
1440 +       unsigned int irq;
1441 +       u8 is_connected;
1442 +       u8 is_suspended;
1443 +
1444 +       spinlock_t tx_lock;
1445 +
1446 +       struct tx_buffer *tx_buffers[TPMIF_TX_RING_SIZE];
1447 +
1448 +       atomic_t tx_busy;
1449 +       void *tx_remember;
1450 +
1451 +       domid_t backend_id;
1452 +       wait_queue_head_t wait_q;
1453 +
1454 +       struct xenbus_device *dev;
1455 +       int ring_ref;
1456 +};
1457 +
1458 +struct tx_buffer {
1459 +       unsigned int size;      // available space in data
1460 +       unsigned int len;       // used space in data
1461 +       unsigned char *data;    // pointer to a page
1462 +};
1463 +
1464 +
1465 +/* locally visible variables */
1466 +static grant_ref_t gref_head;
1467 +static struct tpm_private *my_priv;
1468 +
1469 +/* local function prototypes */
1470 +static irqreturn_t tpmif_int(int irq,
1471 +                             void *tpm_priv,
1472 +                             struct pt_regs *ptregs);
1473 +static void tpmif_rx_action(unsigned long unused);
1474 +static int tpmif_connect(struct xenbus_device *dev,
1475 +                         struct tpm_private *tp,
1476 +                         domid_t domid);
1477 +static DECLARE_TASKLET(tpmif_rx_tasklet, tpmif_rx_action, 0);
1478 +static int tpmif_allocate_tx_buffers(struct tpm_private *tp);
1479 +static void tpmif_free_tx_buffers(struct tpm_private *tp);
1480 +static void tpmif_set_connected_state(struct tpm_private *tp,
1481 +                                      u8 newstate);
1482 +static int tpm_xmit(struct tpm_private *tp,
1483 +                    const u8 * buf, size_t count, int userbuffer,
1484 +                    void *remember);
1485 +static void destroy_tpmring(struct tpm_private *tp);
1486 +void __exit tpmif_exit(void);
1487 +
1488 +#define DPRINTK(fmt, args...) \
1489 +    pr_debug("xen_tpm_fr (%s:%d) " fmt, __FUNCTION__, __LINE__, ##args)
1490 +#define IPRINTK(fmt, args...) \
1491 +    printk(KERN_INFO "xen_tpm_fr: " fmt, ##args)
1492 +#define WPRINTK(fmt, args...) \
1493 +    printk(KERN_WARNING "xen_tpm_fr: " fmt, ##args)
1494 +
1495 +#define GRANT_INVALID_REF      0
1496 +
1497 +
1498 +static inline int
1499 +tx_buffer_copy(struct tx_buffer *txb, const u8 *src, int len,
1500 +               int isuserbuffer)
1501 +{
1502 +       int copied = len;
1503 +
1504 +       if (len > txb->size)
1505 +               copied = txb->size;
1506 +       if (isuserbuffer) {
1507 +               if (copy_from_user(txb->data, src, copied))
1508 +                       return -EFAULT;
1509 +       } else {
1510 +               memcpy(txb->data, src, copied);
1511 +       }
1512 +       txb->len = len;
1513 +       return copied;
1514 +}
1515 +
1516 +static inline struct tx_buffer *tx_buffer_alloc(void)
1517 +{
1518 +       struct tx_buffer *txb;
1519 +
1520 +       txb = kzalloc(sizeof(struct tx_buffer), GFP_KERNEL);
1521 +       if (!txb)
1522 +               return NULL;
1523 +
1524 +       txb->len = 0;
1525 +       txb->size = PAGE_SIZE;
1526 +       txb->data = (unsigned char *)__get_free_page(GFP_KERNEL);
1527 +       if (txb->data == NULL) {
1528 +               kfree(txb);
1529 +               txb = NULL;
1530 +       }
1531 +
1532 +       return txb;
1533 +}
1534 +
1535 +
1536 +static inline void tx_buffer_free(struct tx_buffer *txb)
1537 +{
1538 +       if (txb) {
1539 +               free_page((long)txb->data);
1540 +               kfree(txb);
1541 +       }
1542 +}
1543 +
1544 +/**************************************************************
1545 + Utility function for the tpm_private structure
1546 +**************************************************************/
1547 +static void tpm_private_init(struct tpm_private *tp)
1548 +{
1549 +       spin_lock_init(&tp->tx_lock);
1550 +       init_waitqueue_head(&tp->wait_q);
1551 +       atomic_set(&tp->refcnt, 1);
1552 +}
1553 +
1554 +static void tpm_private_put(void)
1555 +{
1556 +       if (!atomic_dec_and_test(&my_priv->refcnt))
1557 +               return;
1558 +
1559 +       tpmif_free_tx_buffers(my_priv);
1560 +       kfree(my_priv);
1561 +       my_priv = NULL;
1562 +}
1563 +
1564 +static struct tpm_private *tpm_private_get(void)
1565 +{
1566 +       int err;
1567 +
1568 +       if (my_priv) {
1569 +               atomic_inc(&my_priv->refcnt);
1570 +               return my_priv;
1571 +       }
1572 +
1573 +       my_priv = kzalloc(sizeof(struct tpm_private), GFP_KERNEL);
1574 +       if (!my_priv)
1575 +               return NULL;
1576 +
1577 +       tpm_private_init(my_priv);
1578 +       err = tpmif_allocate_tx_buffers(my_priv);
1579 +       if (err < 0)
1580 +               tpm_private_put();
1581 +
1582 +       return my_priv;
1583 +}
1584 +
1585 +/**************************************************************
1586 +
1587 + The interface to let the tpm plugin register its callback
1588 + function and send data to another partition using this module
1589 +
1590 +**************************************************************/
1591 +
1592 +static DEFINE_MUTEX(suspend_lock);
1593 +/*
1594 + * Send data via this module by calling this function
1595 + */
1596 +int vtpm_vd_send(struct tpm_private *tp,
1597 +                 const u8 * buf, size_t count, void *ptr)
1598 +{
1599 +       int sent;
1600 +
1601 +       mutex_lock(&suspend_lock);
1602 +       sent = tpm_xmit(tp, buf, count, 0, ptr);
1603 +       mutex_unlock(&suspend_lock);
1604 +
1605 +       return sent;
1606 +}
1607 +
1608 +/**************************************************************
1609 + XENBUS support code
1610 +**************************************************************/
1611 +
1612 +static int setup_tpmring(struct xenbus_device *dev,
1613 +                         struct tpm_private *tp)
1614 +{
1615 +       tpmif_tx_interface_t *sring;
1616 +       int err;
1617 +
1618 +       tp->ring_ref = GRANT_INVALID_REF;
1619 +
1620 +       sring = (void *)__get_free_page(GFP_KERNEL);
1621 +       if (!sring) {
1622 +               xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
1623 +               return -ENOMEM;
1624 +       }
1625 +       tp->tx = sring;
1626 +
1627 +       err = xenbus_grant_ring(dev, virt_to_mfn(tp->tx));
1628 +       if (err < 0) {
1629 +               free_page((unsigned long)sring);
1630 +               tp->tx = NULL;
1631 +               xenbus_dev_fatal(dev, err, "allocating grant reference");
1632 +               goto fail;
1633 +       }
1634 +       tp->ring_ref = err;
1635 +
1636 +       err = tpmif_connect(dev, tp, dev->otherend_id);
1637 +       if (err)
1638 +               goto fail;
1639 +
1640 +       return 0;
1641 +fail:
1642 +       destroy_tpmring(tp);
1643 +       return err;
1644 +}
1645 +
1646 +
1647 +static void destroy_tpmring(struct tpm_private *tp)
1648 +{
1649 +       tpmif_set_connected_state(tp, 0);
1650 +
1651 +       if (tp->ring_ref != GRANT_INVALID_REF) {
1652 +               gnttab_end_foreign_access(tp->ring_ref, (unsigned long)tp->tx);
1653 +               tp->ring_ref = GRANT_INVALID_REF;
1654 +               tp->tx = NULL;
1655 +       }
1656 +
1657 +       if (tp->irq)
1658 +               unbind_from_irqhandler(tp->irq, tp);
1659 +
1660 +       tp->irq = 0;
1661 +}
1662 +
1663 +
1664 +static int talk_to_backend(struct xenbus_device *dev,
1665 +                           struct tpm_private *tp)
1666 +{
1667 +       const char *message = NULL;
1668 +       int err;
1669 +       struct xenbus_transaction xbt;
1670 +
1671 +       err = setup_tpmring(dev, tp);
1672 +       if (err) {
1673 +               xenbus_dev_fatal(dev, err, "setting up ring");
1674 +               goto out;
1675 +       }
1676 +
1677 +again:
1678 +       err = xenbus_transaction_start(&xbt);
1679 +       if (err) {
1680 +               xenbus_dev_fatal(dev, err, "starting transaction");
1681 +               goto destroy_tpmring;
1682 +       }
1683 +
1684 +       err = xenbus_printf(xbt, dev->nodename,
1685 +                           "ring-ref","%u", tp->ring_ref);
1686 +       if (err) {
1687 +               message = "writing ring-ref";
1688 +               goto abort_transaction;
1689 +       }
1690 +
1691 +       err = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
1692 +                           irq_to_evtchn_port(tp->irq));
1693 +       if (err) {
1694 +               message = "writing event-channel";
1695 +               goto abort_transaction;
1696 +       }
1697 +
1698 +       err = xenbus_transaction_end(xbt, 0);
1699 +       if (err == -EAGAIN)
1700 +               goto again;
1701 +       if (err) {
1702 +               xenbus_dev_fatal(dev, err, "completing transaction");
1703 +               goto destroy_tpmring;
1704 +       }
1705 +
1706 +       xenbus_switch_state(dev, XenbusStateConnected);
1707 +
1708 +       return 0;
1709 +
1710 +abort_transaction:
1711 +       xenbus_transaction_end(xbt, 1);
1712 +       if (message)
1713 +               xenbus_dev_error(dev, err, "%s", message);
1714 +destroy_tpmring:
1715 +       destroy_tpmring(tp);
1716 +out:
1717 +       return err;
1718 +}
1719 +
1720 +/**
1721 + * Callback received when the backend's state changes.
1722 + */
1723 +static void backend_changed(struct xenbus_device *dev,
1724 +                           enum xenbus_state backend_state)
1725 +{
1726 +       struct tpm_private *tp = tpm_private_from_dev(&dev->dev);
1727 +       DPRINTK("\n");
1728 +
1729 +       switch (backend_state) {
1730 +       case XenbusStateInitialising:
1731 +       case XenbusStateInitWait:
1732 +       case XenbusStateInitialised:
1733 +       case XenbusStateReconfiguring:
1734 +       case XenbusStateReconfigured:
1735 +       case XenbusStateUnknown:
1736 +               break;
1737 +
1738 +       case XenbusStateConnected:
1739 +               tpmif_set_connected_state(tp, 1);
1740 +               break;
1741 +
1742 +       case XenbusStateClosing:
1743 +               tpmif_set_connected_state(tp, 0);
1744 +               xenbus_frontend_closed(dev);
1745 +               break;
1746 +
1747 +       case XenbusStateClosed:
1748 +               tpmif_set_connected_state(tp, 0);
1749 +               if (tp->is_suspended == 0)
1750 +                       device_unregister(&dev->dev);
1751 +               xenbus_frontend_closed(dev);
1752 +               break;
1753 +       }
1754 +}
1755 +
1756 +static int tpmfront_probe(struct xenbus_device *dev,
1757 +                          const struct xenbus_device_id *id)
1758 +{
1759 +       int err;
1760 +       int handle;
1761 +       struct tpm_private *tp = tpm_private_get();
1762 +
1763 +       if (!tp)
1764 +               return -ENOMEM;
1765 +
1766 +       tp->chip = init_vtpm(&dev->dev, tp);
1767 +       if (IS_ERR(tp->chip))
1768 +               return PTR_ERR(tp->chip);
1769 +
1770 +       err = xenbus_scanf(XBT_NIL, dev->nodename,
1771 +                          "handle", "%i", &handle);
1772 +       if (XENBUS_EXIST_ERR(err))
1773 +               return err;
1774 +
1775 +       if (err < 0) {
1776 +               xenbus_dev_fatal(dev,err,"reading virtual-device");
1777 +               return err;
1778 +       }
1779 +
1780 +       tp->dev = dev;
1781 +
1782 +       err = talk_to_backend(dev, tp);
1783 +       if (err) {
1784 +               tpm_private_put();
1785 +               return err;
1786 +       }
1787 +
1788 +       return 0;
1789 +}
1790 +
1791 +
1792 +static int tpmfront_remove(struct xenbus_device *dev)
1793 +{
1794 +       struct tpm_private *tp = tpm_private_from_dev(&dev->dev);
1795 +       destroy_tpmring(tp);
1796 +       cleanup_vtpm(&dev->dev);
1797 +       return 0;
1798 +}
1799 +
1800 +static int tpmfront_suspend(struct xenbus_device *dev)
1801 +{
1802 +       struct tpm_private *tp = tpm_private_from_dev(&dev->dev);
1803 +       u32 ctr;
1804 +
1805 +       /* Take the lock, preventing any application from sending. */
1806 +       mutex_lock(&suspend_lock);
1807 +       tp->is_suspended = 1;
1808 +
1809 +       for (ctr = 0; atomic_read(&tp->tx_busy); ctr++) {
1810 +               if ((ctr % 10) == 0)
1811 +                       printk("TPM-FE [INFO]: Waiting for outstanding "
1812 +                              "request.\n");
1813 +               /* Wait for a request to be responded to. */
1814 +               interruptible_sleep_on_timeout(&tp->wait_q, 100);
1815 +       }
1816 +
1817 +       return 0;
1818 +}
1819 +
1820 +static int tpmfront_suspend_finish(struct tpm_private *tp)
1821 +{
1822 +       tp->is_suspended = 0;
1823 +       /* Allow applications to send again. */
1824 +       mutex_unlock(&suspend_lock);
1825 +       return 0;
1826 +}
1827 +
1828 +static int tpmfront_suspend_cancel(struct xenbus_device *dev)
1829 +{
1830 +       struct tpm_private *tp = tpm_private_from_dev(&dev->dev);
1831 +       return tpmfront_suspend_finish(tp);
1832 +}
1833 +
1834 +static int tpmfront_resume(struct xenbus_device *dev)
1835 +{
1836 +       struct tpm_private *tp = tpm_private_from_dev(&dev->dev);
1837 +       destroy_tpmring(tp);
1838 +       return talk_to_backend(dev, tp);
1839 +}
1840 +
1841 +static int tpmif_connect(struct xenbus_device *dev,
1842 +                         struct tpm_private *tp,
1843 +                         domid_t domid)
1844 +{
1845 +       int err;
1846 +
1847 +       tp->backend_id = domid;
1848 +
1849 +       err = bind_listening_port_to_irqhandler(
1850 +               domid, tpmif_int, SA_SAMPLE_RANDOM, "tpmif", tp);
1851 +       if (err <= 0) {
1852 +               WPRINTK("bind_listening_port_to_irqhandler failed "
1853 +                       "(err=%d)\n", err);
1854 +               return err;
1855 +       }
1856 +       tp->irq = err;
1857 +
1858 +       return 0;
1859 +}
1860 +
1861 +static struct xenbus_device_id tpmfront_ids[] = {
1862 +       { "vtpm" },
1863 +       { "" }
1864 +};
1865 +
1866 +static struct xenbus_driver tpmfront = {
1867 +       .name = "vtpm",
1868 +       .owner = THIS_MODULE,
1869 +       .ids = tpmfront_ids,
1870 +       .probe = tpmfront_probe,
1871 +       .remove =  tpmfront_remove,
1872 +       .resume = tpmfront_resume,
1873 +       .otherend_changed = backend_changed,
1874 +       .suspend = tpmfront_suspend,
1875 +       .suspend_cancel = tpmfront_suspend_cancel,
1876 +};
1877 +
1878 +static void __init init_tpm_xenbus(void)
1879 +{
1880 +       xenbus_register_frontend(&tpmfront);
1881 +}
1882 +
1883 +static int tpmif_allocate_tx_buffers(struct tpm_private *tp)
1884 +{
1885 +       unsigned int i;
1886 +
1887 +       for (i = 0; i < TPMIF_TX_RING_SIZE; i++) {
1888 +               tp->tx_buffers[i] = tx_buffer_alloc();
1889 +               if (!tp->tx_buffers[i]) {
1890 +                       tpmif_free_tx_buffers(tp);
1891 +                       return -ENOMEM;
1892 +               }
1893 +       }
1894 +       return 0;
1895 +}
1896 +
1897 +static void tpmif_free_tx_buffers(struct tpm_private *tp)
1898 +{
1899 +       unsigned int i;
1900 +
1901 +       for (i = 0; i < TPMIF_TX_RING_SIZE; i++)
1902 +               tx_buffer_free(tp->tx_buffers[i]);
1903 +}
1904 +
1905 +static void tpmif_rx_action(unsigned long priv)
1906 +{
1907 +       struct tpm_private *tp = (struct tpm_private *)priv;
1908 +       int i = 0;
1909 +       unsigned int received;
1910 +       unsigned int offset = 0;
1911 +       u8 *buffer;
1912 +       tpmif_tx_request_t *tx = &tp->tx->ring[i].req;
1913 +
1914 +       atomic_set(&tp->tx_busy, 0);
1915 +       wake_up_interruptible(&tp->wait_q);
1916 +
1917 +       received = tx->size;
1918 +
1919 +       buffer = kmalloc(received, GFP_ATOMIC);
1920 +       if (!buffer)
1921 +               return;
1922 +
1923 +       for (i = 0; i < TPMIF_TX_RING_SIZE && offset < received; i++) {
1924 +               struct tx_buffer *txb = tp->tx_buffers[i];
1925 +               tpmif_tx_request_t *tx;
1926 +               unsigned int tocopy;
1927 +
1928 +               tx = &tp->tx->ring[i].req;
1929 +               tocopy = tx->size;
1930 +               if (tocopy > PAGE_SIZE)
1931 +                       tocopy = PAGE_SIZE;
1932 +
1933 +               memcpy(&buffer[offset], txb->data, tocopy);
1934 +
1935 +               gnttab_release_grant_reference(&gref_head, tx->ref);
1936 +
1937 +               offset += tocopy;
1938 +       }
1939 +
1940 +       vtpm_vd_recv(tp->chip, buffer, received, tp->tx_remember);
1941 +       kfree(buffer);
1942 +}
1943 +
1944 +
1945 +static irqreturn_t tpmif_int(int irq, void *tpm_priv, struct pt_regs *ptregs)
1946 +{
1947 +       struct tpm_private *tp = tpm_priv;
1948 +       unsigned long flags;
1949 +
1950 +       spin_lock_irqsave(&tp->tx_lock, flags);
1951 +       tpmif_rx_tasklet.data = (unsigned long)tp;
1952 +       tasklet_schedule(&tpmif_rx_tasklet);
1953 +       spin_unlock_irqrestore(&tp->tx_lock, flags);
1954 +
1955 +       return IRQ_HANDLED;
1956 +}
1957 +
1958 +
1959 +static int tpm_xmit(struct tpm_private *tp,
1960 +                    const u8 * buf, size_t count, int isuserbuffer,
1961 +                    void *remember)
1962 +{
1963 +       tpmif_tx_request_t *tx;
1964 +       TPMIF_RING_IDX i;
1965 +       unsigned int offset = 0;
1966 +
1967 +       spin_lock_irq(&tp->tx_lock);
1968 +
1969 +       if (unlikely(atomic_read(&tp->tx_busy))) {
1970 +               printk("tpm_xmit: There's an outstanding request/response "
1971 +                      "on the way!\n");
1972 +               spin_unlock_irq(&tp->tx_lock);
1973 +               return -EBUSY;
1974 +       }
1975 +
1976 +       if (tp->is_connected != 1) {
1977 +               spin_unlock_irq(&tp->tx_lock);
1978 +               return -EIO;
1979 +       }
1980 +
1981 +       for (i = 0; count > 0 && i < TPMIF_TX_RING_SIZE; i++) {
1982 +               struct tx_buffer *txb = tp->tx_buffers[i];
1983 +               int copied;
1984 +
1985 +               if (!txb) {
1986 +                       DPRINTK("txb (i=%d) is NULL. buffers initilized?\n"
1987 +                               "Not transmitting anything!\n", i);
1988 +                       spin_unlock_irq(&tp->tx_lock);
1989 +                       return -EFAULT;
1990 +               }
1991 +
1992 +               copied = tx_buffer_copy(txb, &buf[offset], count,
1993 +                                       isuserbuffer);
1994 +               if (copied < 0) {
1995 +                       /* An error occurred */
1996 +                       spin_unlock_irq(&tp->tx_lock);
1997 +                       return copied;
1998 +               }
1999 +               count -= copied;
2000 +               offset += copied;
2001 +
2002 +               tx = &tp->tx->ring[i].req;
2003 +               tx->addr = virt_to_machine(txb->data);
2004 +               tx->size = txb->len;
2005 +               tx->unused = 0;
2006 +
2007 +               DPRINTK("First 4 characters sent by TPM-FE are "
2008 +                       "0x%02x 0x%02x 0x%02x 0x%02x\n",
2009 +                       txb->data[0],txb->data[1],txb->data[2],txb->data[3]);
2010 +
2011 +               /* Get the granttable reference for this page. */
2012 +               tx->ref = gnttab_claim_grant_reference(&gref_head);
2013 +               if (tx->ref == -ENOSPC) {
2014 +                       spin_unlock_irq(&tp->tx_lock);
2015 +                       DPRINTK("Grant table claim reference failed in "
2016 +                               "func:%s line:%d file:%s\n",
2017 +                               __FUNCTION__, __LINE__, __FILE__);
2018 +                       return -ENOSPC;
2019 +               }
2020 +               gnttab_grant_foreign_access_ref(tx->ref,
2021 +                                               tp->backend_id,
2022 +                                               virt_to_mfn(txb->data),
2023 +                                               0 /*RW*/);
2024 +               wmb();
2025 +       }
2026 +
2027 +       atomic_set(&tp->tx_busy, 1);
2028 +       tp->tx_remember = remember;
2029 +
2030 +       mb();
2031 +
2032 +       notify_remote_via_irq(tp->irq);
2033 +
2034 +       spin_unlock_irq(&tp->tx_lock);
2035 +       return offset;
2036 +}
2037 +
2038 +
2039 +static void tpmif_notify_upperlayer(struct tpm_private *tp)
2040 +{
2041 +       /* Notify upper layer about the state of the connection to the BE. */
2042 +       vtpm_vd_status(tp->chip, (tp->is_connected
2043 +                                 ? TPM_VD_STATUS_CONNECTED
2044 +                                 : TPM_VD_STATUS_DISCONNECTED));
2045 +}
2046 +
2047 +
2048 +static void tpmif_set_connected_state(struct tpm_private *tp, u8 is_connected)
2049 +{
2050 +       /*
2051 +        * Don't notify upper layer if we are in suspend mode and
2052 +        * should disconnect - assumption is that we will resume
2053 +        * The mutex keeps apps from sending.
2054 +        */
2055 +       if (is_connected == 0 && tp->is_suspended == 1)
2056 +               return;
2057 +
2058 +       /*
2059 +        * Unlock the mutex if we are connected again
2060 +        * after being suspended - now resuming.
2061 +        * This also removes the suspend state.
2062 +        */
2063 +       if (is_connected == 1 && tp->is_suspended == 1)
2064 +               tpmfront_suspend_finish(tp);
2065 +
2066 +       if (is_connected != tp->is_connected) {
2067 +               tp->is_connected = is_connected;
2068 +               tpmif_notify_upperlayer(tp);
2069 +       }
2070 +}
2071 +
2072 +
2073 +
2074 +/* =================================================================
2075 + * Initialization function.
2076 + * =================================================================
2077 + */
2078 +
2079 +
2080 +static int __init tpmif_init(void)
2081 +{
2082 +       struct tpm_private *tp;
2083 +
2084 +       if (is_initial_xendomain())
2085 +               return -EPERM;
2086 +
2087 +       tp = tpm_private_get();
2088 +       if (!tp)
2089 +               return -ENOMEM;
2090 +
2091 +       IPRINTK("Initialising the vTPM driver.\n");
2092 +       if (gnttab_alloc_grant_references(TPMIF_TX_RING_SIZE,
2093 +                                         &gref_head) < 0) {
2094 +               tpm_private_put();
2095 +               return -EFAULT;
2096 +       }
2097 +
2098 +       init_tpm_xenbus();
2099 +       return 0;
2100 +}
2101 +
2102 +
2103 +module_init(tpmif_init);
2104 +
2105 +MODULE_LICENSE("Dual BSD/GPL");
2106 --- head-2011-10-07.orig/drivers/edac/edac_mc.c 2011-10-07 10:46:17.000000000 +0200
2107 +++ head-2011-10-07/drivers/edac/edac_mc.c      2011-06-30 15:37:01.000000000 +0200
2108 @@ -611,6 +611,10 @@ static void edac_mc_scrub_block(unsigned
2109  
2110         debugf3("%s()\n", __func__);
2111  
2112 +#ifdef CONFIG_XEN
2113 +       page = mfn_to_local_pfn(page);
2114 +#endif
2115 +
2116         /* ECC error page was not in our memory. Ignore it. */
2117         if (!pfn_valid(page))
2118                 return;
2119 --- head-2011-10-07.orig/drivers/firmware/dell_rbu.c    2011-10-07 10:46:17.000000000 +0200
2120 +++ head-2011-10-07/drivers/firmware/dell_rbu.c 2011-04-13 11:25:29.000000000 +0200
2121 @@ -170,9 +170,28 @@ static int create_packet(void *data, siz
2122                         spin_lock(&rbu_data.lock);
2123                         goto out_alloc_packet_array;
2124                 }
2125 +#ifdef CONFIG_XEN
2126 +               if (ordernum && xen_create_contiguous_region(
2127 +                       (unsigned long)packet_data_temp_buf, ordernum, 0)) {
2128 +                       free_pages((unsigned long)packet_data_temp_buf,
2129 +                                  ordernum);
2130 +                       printk(KERN_WARNING
2131 +                               "dell_rbu:%s: failed to adjust new "
2132 +                               "packet\n", __func__);
2133 +                       retval = -ENOMEM;
2134 +                       spin_lock(&rbu_data.lock);
2135 +                       goto out_alloc_packet_array;
2136 +               }
2137 +#endif
2138  
2139 -               if ((unsigned long)virt_to_phys(packet_data_temp_buf)
2140 +               if ((unsigned long)virt_to_bus(packet_data_temp_buf)
2141                                 < allocation_floor) {
2142 +#ifdef CONFIG_XEN
2143 +                       if (ordernum)
2144 +                               xen_destroy_contiguous_region(
2145 +                                       (unsigned long)packet_data_temp_buf,
2146 +                                       ordernum);
2147 +#endif
2148                         pr_debug("packet 0x%lx below floor at 0x%lx.\n",
2149                                         (unsigned long)virt_to_phys(
2150                                                 packet_data_temp_buf),
2151 @@ -186,7 +205,7 @@ static int create_packet(void *data, siz
2152         newpacket->data = packet_data_temp_buf;
2153  
2154         pr_debug("create_packet: newpacket at physical addr %lx\n",
2155 -               (unsigned long)virt_to_phys(newpacket->data));
2156 +               (unsigned long)virt_to_bus(newpacket->data));
2157  
2158         /* packets may not have fixed size */
2159         newpacket->length = length;
2160 @@ -205,7 +224,7 @@ out_alloc_packet_array:
2161         /* always free packet array */
2162         for (;idx>0;idx--) {
2163                 pr_debug("freeing unused packet below floor 0x%lx.\n",
2164 -                       (unsigned long)virt_to_phys(
2165 +                       (unsigned long)virt_to_bus(
2166                                 invalid_addr_packet_array[idx-1]));
2167                 free_pages((unsigned long)invalid_addr_packet_array[idx-1],
2168                         ordernum);
2169 @@ -349,6 +368,13 @@ static void packet_empty_list(void)
2170                  * to make sure there are no stale RBU packets left in memory
2171                  */
2172                 memset(newpacket->data, 0, rbu_data.packetsize);
2173 +#ifdef CONFIG_XEN
2174 +               if (newpacket->ordernum)
2175 +                       xen_destroy_contiguous_region(
2176 +                               (unsigned long)newpacket->data,
2177 +                               newpacket->ordernum);
2178 +#endif
2179 +
2180                 free_pages((unsigned long) newpacket->data,
2181                         newpacket->ordernum);
2182                 kfree(newpacket);
2183 @@ -403,7 +429,9 @@ static int img_update_realloc(unsigned l
2184  {
2185         unsigned char *image_update_buffer = NULL;
2186         unsigned long rc;
2187 +#ifndef CONFIG_XEN
2188         unsigned long img_buf_phys_addr;
2189 +#endif
2190         int ordernum;
2191         int dma_alloc = 0;
2192  
2193 @@ -434,15 +462,19 @@ static int img_update_realloc(unsigned l
2194  
2195         spin_unlock(&rbu_data.lock);
2196  
2197 +#ifndef CONFIG_XEN
2198         ordernum = get_order(size);
2199         image_update_buffer =
2200                 (unsigned char *) __get_free_pages(GFP_KERNEL, ordernum);
2201  
2202         img_buf_phys_addr =
2203 -               (unsigned long) virt_to_phys(image_update_buffer);
2204 +               (unsigned long) virt_to_bus(image_update_buffer);
2205  
2206         if (img_buf_phys_addr > BIOS_SCAN_LIMIT) {
2207                 free_pages((unsigned long) image_update_buffer, ordernum);
2208 +#else
2209 +       {
2210 +#endif
2211                 ordernum = -1;
2212                 image_update_buffer = dma_alloc_coherent(NULL, size,
2213                         &dell_rbu_dmaaddr, GFP_KERNEL);
2214 @@ -695,6 +727,12 @@ static struct bin_attribute rbu_packet_s
2215  static int __init dcdrbu_init(void)
2216  {
2217         int rc;
2218 +
2219 +#ifdef CONFIG_XEN
2220 +       if (!is_initial_xendomain())
2221 +               return -ENODEV;
2222 +#endif
2223 +
2224         spin_lock_init(&rbu_data.lock);
2225  
2226         init_packet_head();
2227 --- head-2011-10-07.orig/drivers/ide/ide-lib.c  2011-10-07 10:46:17.000000000 +0200
2228 +++ head-2011-10-07/drivers/ide/ide-lib.c       2011-04-13 11:25:29.000000000 +0200
2229 @@ -18,12 +18,12 @@ void ide_toggle_bounce(ide_drive_t *driv
2230  {
2231         u64 addr = BLK_BOUNCE_HIGH;     /* dma64_addr_t */
2232  
2233 -       if (!PCI_DMA_BUS_IS_PHYS) {
2234 -               addr = BLK_BOUNCE_ANY;
2235 -       } else if (on && drive->media == ide_disk) {
2236 +       if (on && drive->media == ide_disk) {
2237                 struct device *dev = drive->hwif->dev;
2238  
2239 -               if (dev && dev->dma_mask)
2240 +               if (!PCI_DMA_BUS_IS_PHYS)
2241 +                       addr = BLK_BOUNCE_ANY;
2242 +               else if (dev && dev->dma_mask)
2243                         addr = *dev->dma_mask;
2244         }
2245  
2246 --- head-2011-10-07.orig/drivers/oprofile/buffer_sync.c 2011-10-07 10:46:17.000000000 +0200
2247 +++ head-2011-10-07/drivers/oprofile/buffer_sync.c      2011-06-30 15:37:10.000000000 +0200
2248 @@ -8,6 +8,10 @@
2249   * @author Barry Kasindorf
2250   * @author Robert Richter <robert.richter@amd.com>
2251   *
2252 + * Modified by Aravind Menon for Xen
2253 + * These modifications are:
2254 + * Copyright (C) 2005 Hewlett-Packard Co.
2255 + *
2256   * This is the core of the buffer management. Each
2257   * CPU buffer is processed and entered into the
2258   * global event buffer. Such processing is necessary
2259 @@ -43,6 +47,8 @@ static cpumask_var_t marked_cpus;
2260  static DEFINE_SPINLOCK(task_mortuary);
2261  static void process_task_mortuary(void);
2262  
2263 +static int cpu_current_domain[NR_CPUS];
2264 +
2265  /* Take ownership of the task struct and place it on the
2266   * list for processing. Only after two full buffer syncs
2267   * does the task eventually get freed, because by then
2268 @@ -61,7 +67,6 @@ task_free_notify(struct notifier_block *
2269         return NOTIFY_OK;
2270  }
2271  
2272 -
2273  /* The task is on its way out. A sync of the buffer means we can catch
2274   * any remaining samples for this task.
2275   */
2276 @@ -151,6 +156,11 @@ static void free_all_tasks(void)
2277  int sync_start(void)
2278  {
2279         int err;
2280 +       int i;
2281 +
2282 +       for (i = 0; i < NR_CPUS; i++) {
2283 +               cpu_current_domain[i] = COORDINATOR_DOMAIN;
2284 +       }
2285  
2286         if (!zalloc_cpumask_var(&marked_cpus, GFP_KERNEL))
2287                 return -ENOMEM;
2288 @@ -287,13 +297,29 @@ static void add_cpu_switch(int i)
2289         last_cookie = INVALID_COOKIE;
2290  }
2291  
2292 -static void add_kernel_ctx_switch(unsigned int in_kernel)
2293 +static void add_cpu_mode_switch(unsigned int cpu_mode)
2294  {
2295         add_event_entry(ESCAPE_CODE);
2296 -       if (in_kernel)
2297 +       switch (cpu_mode) {
2298 +       case CPU_MODE_USER:
2299 +               add_event_entry(USER_ENTER_SWITCH_CODE);
2300 +               break;
2301 +       case CPU_MODE_KERNEL:
2302                 add_event_entry(KERNEL_ENTER_SWITCH_CODE);
2303 -       else
2304 -               add_event_entry(KERNEL_EXIT_SWITCH_CODE);
2305 +               break;
2306 +       case CPU_MODE_XEN:
2307 +               add_event_entry(XEN_ENTER_SWITCH_CODE);
2308 +               break;
2309 +       default:
2310 +               break;
2311 +       }
2312 +}
2313 +
2314 +static void add_domain_switch(unsigned long domain_id)
2315 +{
2316 +       add_event_entry(ESCAPE_CODE);
2317 +       add_event_entry(DOMAIN_SWITCH_CODE);
2318 +       add_event_entry(domain_id);
2319  }
2320  
2321  static void
2322 @@ -374,12 +400,12 @@ static inline void add_sample_entry(unsi
2323   * for later lookup from userspace. Return 0 on failure.
2324   */
2325  static int
2326 -add_sample(struct mm_struct *mm, struct op_sample *s, int in_kernel)
2327 +add_sample(struct mm_struct *mm, struct op_sample *s, int cpu_mode)
2328  {
2329         unsigned long cookie;
2330         off_t offset;
2331  
2332 -       if (in_kernel) {
2333 +       if (cpu_mode >= CPU_MODE_KERNEL) {
2334                 add_sample_entry(s->eip, s->event);
2335                 return 1;
2336         }
2337 @@ -504,9 +530,10 @@ void sync_buffer(int cpu)
2338         unsigned long val;
2339         struct task_struct *new;
2340         unsigned long cookie = 0;
2341 -       int in_kernel = 1;
2342 +       int cpu_mode = CPU_MODE_KERNEL;
2343         sync_buffer_state state = sb_buffer_start;
2344         unsigned int i;
2345 +       int domain_switch = 0;
2346         unsigned long available;
2347         unsigned long flags;
2348         struct op_entry entry;
2349 @@ -516,6 +543,11 @@ void sync_buffer(int cpu)
2350  
2351         add_cpu_switch(cpu);
2352  
2353 +       /* We need to assign the first samples in this CPU buffer to the
2354 +          same domain that we were processing at the last sync_buffer */
2355 +       if (cpu_current_domain[cpu] != COORDINATOR_DOMAIN)
2356 +               add_domain_switch(cpu_current_domain[cpu]);
2357 +
2358         op_cpu_buffer_reset(cpu);
2359         available = op_cpu_buffer_entries(cpu);
2360  
2361 @@ -524,6 +556,13 @@ void sync_buffer(int cpu)
2362                 if (!sample)
2363                         break;
2364  
2365 +               if (domain_switch) {
2366 +                       cpu_current_domain[cpu] = sample->eip;
2367 +                       add_domain_switch(sample->eip);
2368 +                       domain_switch = 0;
2369 +                       continue;
2370 +               }
2371 +
2372                 if (is_code(sample->eip)) {
2373                         flags = sample->event;
2374                         if (flags & TRACE_BEGIN) {
2375 @@ -532,10 +571,10 @@ void sync_buffer(int cpu)
2376                         }
2377                         if (flags & KERNEL_CTX_SWITCH) {
2378                                 /* kernel/userspace switch */
2379 -                               in_kernel = flags & IS_KERNEL;
2380 +                               cpu_mode = flags & CPU_MODE_MASK;
2381                                 if (state == sb_buffer_start)
2382                                         state = sb_sample_start;
2383 -                               add_kernel_ctx_switch(flags & IS_KERNEL);
2384 +                               add_cpu_mode_switch(cpu_mode);
2385                         }
2386                         if (flags & USER_CTX_SWITCH
2387                             && op_cpu_buffer_get_data(&entry, &val)) {
2388 @@ -548,16 +587,23 @@ void sync_buffer(int cpu)
2389                                         cookie = get_exec_dcookie(mm);
2390                                 add_user_ctx_switch(new, cookie);
2391                         }
2392 +                       if (flags & DOMAIN_SWITCH)
2393 +                               domain_switch = 1;
2394                         if (op_cpu_buffer_get_size(&entry))
2395                                 add_data(&entry, mm);
2396                         continue;
2397                 }
2398  
2399 +               if (cpu_current_domain[cpu] != COORDINATOR_DOMAIN) {
2400 +                       add_sample_entry(sample->eip, sample->event);
2401 +                       continue;
2402 +               }
2403 +
2404                 if (state < sb_bt_start)
2405                         /* ignore sample */
2406                         continue;
2407  
2408 -               if (add_sample(mm, sample, in_kernel))
2409 +               if (add_sample(mm, sample, cpu_mode))
2410                         continue;
2411  
2412                 /* ignore backtraces if failed to add a sample */
2413 @@ -568,6 +614,10 @@ void sync_buffer(int cpu)
2414         }
2415         release_mm(mm);
2416  
2417 +       /* We reset domain to COORDINATOR at each CPU switch */
2418 +       if (cpu_current_domain[cpu] != COORDINATOR_DOMAIN)
2419 +               add_domain_switch(COORDINATOR_DOMAIN);
2420 +
2421         mark_done(cpu);
2422  
2423         mutex_unlock(&buffer_mutex);
2424 --- head-2011-10-07.orig/drivers/oprofile/cpu_buffer.c  2011-10-07 10:46:17.000000000 +0200
2425 +++ head-2011-10-07/drivers/oprofile/cpu_buffer.c       2011-04-13 11:25:29.000000000 +0200
2426 @@ -8,6 +8,10 @@
2427   * @author Barry Kasindorf <barry.kasindorf@amd.com>
2428   * @author Robert Richter <robert.richter@amd.com>
2429   *
2430 + * Modified by Aravind Menon for Xen
2431 + * These modifications are:
2432 + * Copyright (C) 2005 Hewlett-Packard Co.
2433 + *
2434   * Each CPU has a local buffer that stores PC value/event
2435   * pairs. We also log context switches when we notice them.
2436   * Eventually each CPU's buffer is processed into the global
2437 @@ -38,6 +42,8 @@ static void wq_sync_buffer(struct work_s
2438  #define DEFAULT_TIMER_EXPIRE (HZ / 10)
2439  static int work_enabled;
2440  
2441 +static int32_t current_domain = COORDINATOR_DOMAIN;
2442 +
2443  unsigned long oprofile_get_cpu_buffer_size(void)
2444  {
2445         return oprofile_cpu_buffer_size;
2446 @@ -75,7 +81,7 @@ int alloc_cpu_buffers(void)
2447                 struct oprofile_cpu_buffer *b = &per_cpu(op_cpu_buffer, i);
2448  
2449                 b->last_task = NULL;
2450 -               b->last_is_kernel = -1;
2451 +               b->last_cpu_mode = -1;
2452                 b->tracing = 0;
2453                 b->buffer_size = buffer_size;
2454                 b->sample_received = 0;
2455 @@ -180,7 +186,7 @@ unsigned long op_cpu_buffer_entries(int 
2456  
2457  static int
2458  op_add_code(struct oprofile_cpu_buffer *cpu_buf, unsigned long backtrace,
2459 -           int is_kernel, struct task_struct *task)
2460 +           int cpu_mode, struct task_struct *task)
2461  {
2462         struct op_entry entry;
2463         struct op_sample *sample;
2464 @@ -193,16 +199,15 @@ op_add_code(struct oprofile_cpu_buffer *
2465                 flags |= TRACE_BEGIN;
2466  
2467         /* notice a switch from user->kernel or vice versa */
2468 -       is_kernel = !!is_kernel;
2469 -       if (cpu_buf->last_is_kernel != is_kernel) {
2470 -               cpu_buf->last_is_kernel = is_kernel;
2471 -               flags |= KERNEL_CTX_SWITCH;
2472 -               if (is_kernel)
2473 -                       flags |= IS_KERNEL;
2474 +       if (cpu_buf->last_cpu_mode != cpu_mode) {
2475 +               cpu_buf->last_cpu_mode = cpu_mode;
2476 +               flags |= KERNEL_CTX_SWITCH | cpu_mode;
2477         }
2478  
2479         /* notice a task switch */
2480 -       if (cpu_buf->last_task != task) {
2481 +       /* if not processing other domain samples */
2482 +       if (cpu_buf->last_task != task &&
2483 +           current_domain == COORDINATOR_DOMAIN) {
2484                 cpu_buf->last_task = task;
2485                 flags |= USER_CTX_SWITCH;
2486         }
2487 @@ -251,14 +256,14 @@ op_add_sample(struct oprofile_cpu_buffer
2488  /*
2489   * This must be safe from any context.
2490   *
2491 - * is_kernel is needed because on some architectures you cannot
2492 + * cpu_mode is needed because on some architectures you cannot
2493   * tell if you are in kernel or user space simply by looking at
2494 - * pc. We tag this in the buffer by generating kernel enter/exit
2495 - * events whenever is_kernel changes
2496 + * pc. We tag this in the buffer by generating kernel/user (and
2497 + * xen) enter events whenever cpu_mode changes
2498   */
2499  static int
2500  log_sample(struct oprofile_cpu_buffer *cpu_buf, unsigned long pc,
2501 -          unsigned long backtrace, int is_kernel, unsigned long event,
2502 +          unsigned long backtrace, int cpu_mode, unsigned long event,
2503            struct task_struct *task)
2504  {
2505         struct task_struct *tsk = task ? task : current;
2506 @@ -269,7 +274,7 @@ log_sample(struct oprofile_cpu_buffer *c
2507                 return 0;
2508         }
2509  
2510 -       if (op_add_code(cpu_buf, backtrace, is_kernel, tsk))
2511 +       if (op_add_code(cpu_buf, backtrace, cpu_mode, tsk))
2512                 goto fail;
2513  
2514         if (op_add_sample(cpu_buf, pc, event))
2515 @@ -440,6 +445,25 @@ fail:
2516         return;
2517  }
2518  
2519 +int oprofile_add_domain_switch(int32_t domain_id)
2520 +{
2521 +       struct oprofile_cpu_buffer * cpu_buf = &cpu_buffer[smp_processor_id()];
2522 +
2523 +       /* should have space for switching into and out of domain
2524 +          (2 slots each) plus one sample and one cpu mode switch */
2525 +       if (((nr_available_slots(cpu_buf) < 6) &&
2526 +            (domain_id != COORDINATOR_DOMAIN)) ||
2527 +           (nr_available_slots(cpu_buf) < 2))
2528 +               return 0;
2529 +
2530 +       add_code(cpu_buf, DOMAIN_SWITCH);
2531 +       add_sample(cpu_buf, domain_id, 0);
2532 +
2533 +       current_domain = domain_id;
2534 +
2535 +       return 1;
2536 +}
2537 +
2538  /*
2539   * This serves to avoid cpu buffer overflow, and makes sure
2540   * the task mortuary progresses
2541 --- head-2011-10-07.orig/drivers/oprofile/cpu_buffer.h  2011-10-07 10:46:17.000000000 +0200
2542 +++ head-2011-10-07/drivers/oprofile/cpu_buffer.h       2011-04-13 11:25:29.000000000 +0200
2543 @@ -41,7 +41,7 @@ struct op_entry;
2544  struct oprofile_cpu_buffer {
2545         unsigned long buffer_size;
2546         struct task_struct *last_task;
2547 -       int last_is_kernel;
2548 +       int last_cpu_mode;
2549         int tracing;
2550         unsigned long sample_received;
2551         unsigned long sample_lost_overflow;
2552 @@ -63,7 +63,7 @@ static inline void op_cpu_buffer_reset(i
2553  {
2554         struct oprofile_cpu_buffer *cpu_buf = &per_cpu(op_cpu_buffer, cpu);
2555  
2556 -       cpu_buf->last_is_kernel = -1;
2557 +       cpu_buf->last_cpu_mode = -1;
2558         cpu_buf->last_task = NULL;
2559  }
2560  
2561 @@ -113,9 +113,13 @@ int op_cpu_buffer_get_data(struct op_ent
2562  }
2563  
2564  /* extra data flags */
2565 -#define KERNEL_CTX_SWITCH      (1UL << 0)
2566 -#define IS_KERNEL              (1UL << 1)
2567 +#define CPU_MODE_USER          0
2568 +#define CPU_MODE_KERNEL                1
2569 +#define CPU_MODE_XEN           2
2570 +#define CPU_MODE_MASK          3
2571  #define TRACE_BEGIN            (1UL << 2)
2572  #define USER_CTX_SWITCH                (1UL << 3)
2573 +#define KERNEL_CTX_SWITCH      (1UL << 4)
2574 +#define DOMAIN_SWITCH          (1UL << 5)
2575  
2576  #endif /* OPROFILE_CPU_BUFFER_H */
2577 --- head-2011-10-07.orig/drivers/oprofile/event_buffer.h        2011-10-07 10:46:17.000000000 +0200
2578 +++ head-2011-10-07/drivers/oprofile/event_buffer.h     2011-04-13 11:25:29.000000000 +0200
2579 @@ -30,6 +30,9 @@ void wake_up_buffer_waiter(void);
2580  #define INVALID_COOKIE ~0UL
2581  #define NO_COOKIE 0UL
2582  
2583 +/* Constant used to refer to coordinator domain (Xen) */
2584 +#define COORDINATOR_DOMAIN -1
2585 +
2586  extern const struct file_operations event_buffer_fops;
2587  
2588  /* mutex between sync_cpu_buffers() and the
2589 --- head-2011-10-07.orig/drivers/oprofile/oprof.c       2011-10-07 10:46:17.000000000 +0200
2590 +++ head-2011-10-07/drivers/oprofile/oprof.c    2011-04-13 11:25:29.000000000 +0200
2591 @@ -5,6 +5,10 @@
2592   * @remark Read the file COPYING
2593   *
2594   * @author John Levon <levon@movementarian.org>
2595 + *
2596 + * Modified by Aravind Menon for Xen
2597 + * These modifications are:
2598 + * Copyright (C) 2005 Hewlett-Packard Co.
2599   */
2600  
2601  #include <linux/kernel.h>
2602 @@ -35,6 +39,32 @@ static DEFINE_MUTEX(start_mutex);
2603   */
2604  static int timer = 0;
2605  
2606 +int oprofile_set_active(int active_domains[], unsigned int adomains)
2607 +{
2608 +       int err;
2609 +
2610 +       if (!oprofile_ops.set_active)
2611 +               return -EINVAL;
2612 +
2613 +       mutex_lock(&start_mutex);
2614 +       err = oprofile_ops.set_active(active_domains, adomains);
2615 +       mutex_unlock(&start_mutex);
2616 +       return err;
2617 +}
2618 +
2619 +int oprofile_set_passive(int passive_domains[], unsigned int pdomains)
2620 +{
2621 +       int err;
2622 +
2623 +       if (!oprofile_ops.set_passive)
2624 +               return -EINVAL;
2625 +
2626 +       mutex_lock(&start_mutex);
2627 +       err = oprofile_ops.set_passive(passive_domains, pdomains);
2628 +       mutex_unlock(&start_mutex);
2629 +       return err;
2630 +}
2631 +
2632  int oprofile_setup(void)
2633  {
2634         int err;
2635 --- head-2011-10-07.orig/drivers/oprofile/oprof.h       2011-10-07 10:46:17.000000000 +0200
2636 +++ head-2011-10-07/drivers/oprofile/oprof.h    2011-04-13 11:25:29.000000000 +0200
2637 @@ -40,4 +40,7 @@ void oprofile_timer_exit(void);
2638  int oprofile_set_ulong(unsigned long *addr, unsigned long val);
2639  int oprofile_set_timeout(unsigned long time);
2640  
2641 +int oprofile_set_active(int active_domains[], unsigned int adomains);
2642 +int oprofile_set_passive(int passive_domains[], unsigned int pdomains);
2643 +
2644  #endif /* OPROF_H */
2645 --- head-2011-10-07.orig/drivers/oprofile/oprofile_files.c      2011-10-07 10:46:17.000000000 +0200
2646 +++ head-2011-10-07/drivers/oprofile/oprofile_files.c   2011-04-13 11:25:29.000000000 +0200
2647 @@ -5,11 +5,17 @@
2648   * @remark Read the file COPYING
2649   *
2650   * @author John Levon <levon@movementarian.org>
2651 + *
2652 + * Modified by Aravind Menon for Xen
2653 + * These modifications are:
2654 + * Copyright (C) 2005 Hewlett-Packard Co.
2655   */
2656  
2657  #include <linux/fs.h>
2658  #include <linux/oprofile.h>
2659  #include <linux/jiffies.h>
2660 +#include <asm/uaccess.h>
2661 +#include <linux/ctype.h>
2662  
2663  #include "event_buffer.h"
2664  #include "oprofile_stats.h"
2665 @@ -174,6 +180,195 @@ static const struct file_operations dump
2666         .llseek         = noop_llseek,
2667  };
2668  
2669 +#define TMPBUFSIZE 512
2670 +
2671 +static unsigned int adomains = 0;
2672 +static int active_domains[MAX_OPROF_DOMAINS + 1];
2673 +static DEFINE_MUTEX(adom_mutex);
2674 +
2675 +static ssize_t adomain_write(struct file * file, char const __user * buf,
2676 +                            size_t count, loff_t * offset)
2677 +{
2678 +       char *tmpbuf;
2679 +       char *startp, *endp;
2680 +       int i;
2681 +       unsigned long val;
2682 +       ssize_t retval = count;
2683 +
2684 +       if (*offset)
2685 +               return -EINVAL;
2686 +       if (count > TMPBUFSIZE - 1)
2687 +               return -EINVAL;
2688 +
2689 +       if (!(tmpbuf = kmalloc(TMPBUFSIZE, GFP_KERNEL)))
2690 +               return -ENOMEM;
2691 +
2692 +       if (copy_from_user(tmpbuf, buf, count)) {
2693 +               kfree(tmpbuf);
2694 +               return -EFAULT;
2695 +       }
2696 +       tmpbuf[count] = 0;
2697 +
2698 +       mutex_lock(&adom_mutex);
2699 +
2700 +       startp = tmpbuf;
2701 +       /* Parse one more than MAX_OPROF_DOMAINS, for easy error checking */
2702 +       for (i = 0; i <= MAX_OPROF_DOMAINS; i++) {
2703 +               val = simple_strtoul(startp, &endp, 0);
2704 +               if (endp == startp)
2705 +                       break;
2706 +               while (ispunct(*endp) || isspace(*endp))
2707 +                       endp++;
2708 +               active_domains[i] = val;
2709 +               if (active_domains[i] != val)
2710 +                       /* Overflow, force error below */
2711 +                       i = MAX_OPROF_DOMAINS + 1;
2712 +               startp = endp;
2713 +       }
2714 +       /* Force error on trailing junk */
2715 +       adomains = *startp ? MAX_OPROF_DOMAINS + 1 : i;
2716 +
2717 +       kfree(tmpbuf);
2718 +
2719 +       if (adomains > MAX_OPROF_DOMAINS
2720 +           || oprofile_set_active(active_domains, adomains)) {
2721 +               adomains = 0;
2722 +               retval = -EINVAL;
2723 +       }
2724 +
2725 +       mutex_unlock(&adom_mutex);
2726 +       return retval;
2727 +}
2728 +
2729 +static ssize_t adomain_read(struct file * file, char __user * buf,
2730 +                           size_t count, loff_t * offset)
2731 +{
2732 +       char * tmpbuf;
2733 +       size_t len;
2734 +       int i;
2735 +       ssize_t retval;
2736 +
2737 +       if (!(tmpbuf = kmalloc(TMPBUFSIZE, GFP_KERNEL)))
2738 +               return -ENOMEM;
2739 +
2740 +       mutex_lock(&adom_mutex);
2741 +
2742 +       len = 0;
2743 +       for (i = 0; i < adomains; i++)
2744 +               len += snprintf(tmpbuf + len,
2745 +                               len < TMPBUFSIZE ? TMPBUFSIZE - len : 0,
2746 +                               "%u ", active_domains[i]);
2747 +       WARN_ON(len > TMPBUFSIZE);
2748 +       if (len != 0 && len <= TMPBUFSIZE)
2749 +               tmpbuf[len-1] = '\n';
2750 +
2751 +       mutex_unlock(&adom_mutex);
2752 +
2753 +       retval = simple_read_from_buffer(buf, count, offset, tmpbuf, len);
2754 +
2755 +       kfree(tmpbuf);
2756 +       return retval;
2757 +}
2758 +
2759 +
2760 +static const struct file_operations active_domain_ops = {
2761 +       .read           = adomain_read,
2762 +       .write          = adomain_write,
2763 +};
2764 +
2765 +static unsigned int pdomains = 0;
2766 +static int passive_domains[MAX_OPROF_DOMAINS];
2767 +static DEFINE_MUTEX(pdom_mutex);
2768 +
2769 +static ssize_t pdomain_write(struct file * file, char const __user * buf,
2770 +                            size_t count, loff_t * offset)
2771 +{
2772 +       char *tmpbuf;
2773 +       char *startp, *endp;
2774 +       int i;
2775 +       unsigned long val;
2776 +       ssize_t retval = count;
2777 +
2778 +       if (*offset)
2779 +               return -EINVAL;
2780 +       if (count > TMPBUFSIZE - 1)
2781 +               return -EINVAL;
2782 +
2783 +       if (!(tmpbuf = kmalloc(TMPBUFSIZE, GFP_KERNEL)))
2784 +               return -ENOMEM;
2785 +
2786 +       if (copy_from_user(tmpbuf, buf, count)) {
2787 +               kfree(tmpbuf);
2788 +               return -EFAULT;
2789 +       }
2790 +       tmpbuf[count] = 0;
2791 +
2792 +       mutex_lock(&pdom_mutex);
2793 +
2794 +       startp = tmpbuf;
2795 +       /* Parse one more than MAX_OPROF_DOMAINS, for easy error checking */
2796 +       for (i = 0; i <= MAX_OPROF_DOMAINS; i++) {
2797 +               val = simple_strtoul(startp, &endp, 0);
2798 +               if (endp == startp)
2799 +                       break;
2800 +               while (ispunct(*endp) || isspace(*endp))
2801 +                       endp++;
2802 +               passive_domains[i] = val;
2803 +               if (passive_domains[i] != val)
2804 +                       /* Overflow, force error below */
2805 +                       i = MAX_OPROF_DOMAINS + 1;
2806 +               startp = endp;
2807 +       }
2808 +       /* Force error on trailing junk */
2809 +       pdomains = *startp ? MAX_OPROF_DOMAINS + 1 : i;
2810 +
2811 +       kfree(tmpbuf);
2812 +
2813 +       if (pdomains > MAX_OPROF_DOMAINS
2814 +           || oprofile_set_passive(passive_domains, pdomains)) {
2815 +               pdomains = 0;
2816 +               retval = -EINVAL;
2817 +       }
2818 +
2819 +       mutex_unlock(&pdom_mutex);
2820 +       return retval;
2821 +}
2822 +
2823 +static ssize_t pdomain_read(struct file * file, char __user * buf,
2824 +                           size_t count, loff_t * offset)
2825 +{
2826 +       char * tmpbuf;
2827 +       size_t len;
2828 +       int i;
2829 +       ssize_t retval;
2830 +
2831 +       if (!(tmpbuf = kmalloc(TMPBUFSIZE, GFP_KERNEL)))
2832 +               return -ENOMEM;
2833 +
2834 +       mutex_lock(&pdom_mutex);
2835 +
2836 +       len = 0;
2837 +       for (i = 0; i < pdomains; i++)
2838 +               len += snprintf(tmpbuf + len,
2839 +                               len < TMPBUFSIZE ? TMPBUFSIZE - len : 0,
2840 +                               "%u ", passive_domains[i]);
2841 +       WARN_ON(len > TMPBUFSIZE);
2842 +       if (len != 0 && len <= TMPBUFSIZE)
2843 +               tmpbuf[len-1] = '\n';
2844 +
2845 +       mutex_unlock(&pdom_mutex);
2846 +
2847 +       retval = simple_read_from_buffer(buf, count, offset, tmpbuf, len);
2848 +
2849 +       kfree(tmpbuf);
2850 +       return retval;
2851 +}
2852 +
2853 +static const struct file_operations passive_domain_ops = {
2854 +       .read           = pdomain_read,
2855 +       .write          = pdomain_write,
2856 +};
2857 +
2858  void oprofile_create_files(struct super_block *sb, struct dentry *root)
2859  {
2860         /* reinitialize default values */
2861 @@ -184,6 +379,8 @@ void oprofile_create_files(struct super_
2862  
2863         oprofilefs_create_file(sb, root, "enable", &enable_fops);
2864         oprofilefs_create_file_perm(sb, root, "dump", &dump_fops, 0666);
2865 +       oprofilefs_create_file(sb, root, "active_domains", &active_domain_ops);
2866 +       oprofilefs_create_file(sb, root, "passive_domains", &passive_domain_ops);
2867         oprofilefs_create_file(sb, root, "buffer", &event_buffer_fops);
2868         oprofilefs_create_ulong(sb, root, "buffer_size", &oprofile_buffer_size);
2869         oprofilefs_create_ulong(sb, root, "buffer_watershed", &oprofile_buffer_watershed);
2870 --- head-2011-10-07.orig/fs/aio.c       2011-10-07 10:46:17.000000000 +0200
2871 +++ head-2011-10-07/fs/aio.c    2011-04-13 11:25:29.000000000 +0200
2872 @@ -39,6 +39,11 @@
2873  #include <asm/kmap_types.h>
2874  #include <asm/uaccess.h>
2875  
2876 +#ifdef CONFIG_EPOLL
2877 +#include <linux/poll.h>
2878 +#include <linux/eventpoll.h>
2879 +#endif
2880 +
2881  #if DEBUG > 1
2882  #define dprintk                printk
2883  #else
2884 @@ -991,6 +996,11 @@ put_rq:
2885         if (waitqueue_active(&ctx->wait))
2886                 wake_up(&ctx->wait);
2887  
2888 +#ifdef CONFIG_EPOLL
2889 +       if (ctx->file && waitqueue_active(&ctx->poll_wait))
2890 +               wake_up(&ctx->poll_wait);
2891 +#endif
2892 +
2893         spin_unlock_irqrestore(&ctx->ctx_lock, flags);
2894         return ret;
2895  }
2896 @@ -999,6 +1009,8 @@ EXPORT_SYMBOL(aio_complete);
2897  /* aio_read_evt
2898   *     Pull an event off of the ioctx's event ring.  Returns the number of 
2899   *     events fetched (0 or 1 ;-)
2900 + *     If ent parameter is 0, just returns the number of events that would
2901 + *     be fetched.
2902   *     FIXME: make this use cmpxchg.
2903   *     TODO: make the ringbuffer user mmap()able (requires FIXME).
2904   */
2905 @@ -1021,13 +1033,18 @@ static int aio_read_evt(struct kioctx *i
2906  
2907         head = ring->head % info->nr;
2908         if (head != ring->tail) {
2909 -               struct io_event *evp = aio_ring_event(info, head, KM_USER1);
2910 -               *ent = *evp;
2911 -               head = (head + 1) % info->nr;
2912 -               smp_mb(); /* finish reading the event before updatng the head */
2913 -               ring->head = head;
2914 -               ret = 1;
2915 -               put_aio_ring_event(evp, KM_USER1);
2916 +               if (ent) { /* event requested */
2917 +                       struct io_event *evp =
2918 +                               aio_ring_event(info, head, KM_USER1);
2919 +                       *ent = *evp;
2920 +                       head = (head + 1) % info->nr;
2921 +                       /* finish reading the event before updatng the head */
2922 +                       smp_mb();
2923 +                       ring->head = head;
2924 +                       ret = 1;
2925 +                       put_aio_ring_event(evp, KM_USER1);
2926 +               } else /* only need to know availability */
2927 +                       ret = 1;
2928         }
2929         spin_unlock(&info->ring_lock);
2930  
2931 @@ -1212,6 +1229,13 @@ static void io_destroy(struct kioctx *io
2932  
2933         aio_cancel_all(ioctx);
2934         wait_for_all_aios(ioctx);
2935 +#ifdef CONFIG_EPOLL
2936 +       /* forget the poll file, but it's up to the user to close it */
2937 +       if (ioctx->file) {
2938 +               ioctx->file->private_data = 0;
2939 +               ioctx->file = 0;
2940 +       }
2941 +#endif
2942  
2943         /*
2944          * Wake up any waiters.  The setting of ctx->dead must be seen
2945 @@ -1222,6 +1246,67 @@ static void io_destroy(struct kioctx *io
2946         put_ioctx(ioctx);       /* once for the lookup */
2947  }
2948  
2949 +#ifdef CONFIG_EPOLL
2950 +
2951 +static int aio_queue_fd_close(struct inode *inode, struct file *file)
2952 +{
2953 +       struct kioctx *ioctx = file->private_data;
2954 +       if (ioctx) {
2955 +               file->private_data = 0;
2956 +               spin_lock_irq(&ioctx->ctx_lock);
2957 +               ioctx->file = 0;
2958 +               spin_unlock_irq(&ioctx->ctx_lock);
2959 +       }
2960 +       return 0;
2961 +}
2962 +
2963 +static unsigned int aio_queue_fd_poll(struct file *file, poll_table *wait)
2964 +{      unsigned int pollflags = 0;
2965 +       struct kioctx *ioctx = file->private_data;
2966 +
2967 +       if (ioctx) {
2968 +
2969 +               spin_lock_irq(&ioctx->ctx_lock);
2970 +               /* Insert inside our poll wait queue */
2971 +               poll_wait(file, &ioctx->poll_wait, wait);
2972 +
2973 +               /* Check our condition */
2974 +               if (aio_read_evt(ioctx, 0))
2975 +                       pollflags = POLLIN | POLLRDNORM;
2976 +               spin_unlock_irq(&ioctx->ctx_lock);
2977 +       }
2978 +
2979 +       return pollflags;
2980 +}
2981 +
2982 +static const struct file_operations aioq_fops = {
2983 +       .release        = aio_queue_fd_close,
2984 +       .poll           = aio_queue_fd_poll
2985 +};
2986 +
2987 +/* make_aio_fd:
2988 + *  Create a file descriptor that can be used to poll the event queue.
2989 + *  Based and piggybacked on the excellent epoll code.
2990 + */
2991 +
2992 +static int make_aio_fd(struct kioctx *ioctx)
2993 +{
2994 +       int error, fd;
2995 +       struct inode *inode;
2996 +       struct file *file;
2997 +
2998 +       error = ep_getfd(&fd, &inode, &file, NULL, &aioq_fops);
2999 +       if (error)
3000 +               return error;
3001 +
3002 +       /* associate the file with the IO context */
3003 +       file->private_data = ioctx;
3004 +       ioctx->file = file;
3005 +       init_waitqueue_head(&ioctx->poll_wait);
3006 +       return fd;
3007 +}
3008 +#endif
3009 +
3010  /* sys_io_setup:
3011   *     Create an aio_context capable of receiving at least nr_events.
3012   *     ctxp must not point to an aio_context that already exists, and
3013 @@ -1234,18 +1319,30 @@ static void io_destroy(struct kioctx *io
3014   *     resources are available.  May fail with -EFAULT if an invalid
3015   *     pointer is passed for ctxp.  Will fail with -ENOSYS if not
3016   *     implemented.
3017 + *
3018 + *     To request a selectable fd, the user context has to be initialized
3019 + *     to 1, instead of 0, and the return value is the fd.
3020 + *     This keeps the system call compatible, since a non-zero value
3021 + *     was not allowed so far.
3022   */
3023  SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
3024  {
3025         struct kioctx *ioctx = NULL;
3026         unsigned long ctx;
3027         long ret;
3028 +       int make_fd = 0;
3029  
3030         ret = get_user(ctx, ctxp);
3031         if (unlikely(ret))
3032                 goto out;
3033  
3034         ret = -EINVAL;
3035 +#ifdef CONFIG_EPOLL
3036 +       if (ctx == 1) {
3037 +               make_fd = 1;
3038 +               ctx = 0;
3039 +       }
3040 +#endif
3041         if (unlikely(ctx || nr_events == 0)) {
3042                 pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
3043                          ctx, nr_events);
3044 @@ -1256,8 +1353,12 @@ SYSCALL_DEFINE2(io_setup, unsigned, nr_e
3045         ret = PTR_ERR(ioctx);
3046         if (!IS_ERR(ioctx)) {
3047                 ret = put_user(ioctx->user_id, ctxp);
3048 -               if (!ret)
3049 -                       return 0;
3050 +#ifdef CONFIG_EPOLL
3051 +               if (make_fd && ret >= 0)
3052 +                       ret = make_aio_fd(ioctx);
3053 +#endif
3054 +               if (ret >= 0)
3055 +                       return ret;
3056