x86/amd-iommu: Add per IOMMU reference counting
[linux/fpc-iii.git] / drivers / ide / ide-acpi.c
blobc0cf45a11b938cfd4b0d0bb84329535f8151172c
1 /*
2 * Provides ACPI support for IDE drives.
4 * Copyright (C) 2005 Intel Corp.
5 * Copyright (C) 2005 Randy Dunlap
6 * Copyright (C) 2006 SUSE Linux Products GmbH
7 * Copyright (C) 2006 Hannes Reinecke
8 */
10 #include <linux/ata.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <acpi/acpi.h>
16 #include <linux/ide.h>
17 #include <linux/pci.h>
18 #include <linux/dmi.h>
20 #include <acpi/acpi_bus.h>
22 #define REGS_PER_GTF 7
24 struct GTM_buffer {
25 u32 PIO_speed0;
26 u32 DMA_speed0;
27 u32 PIO_speed1;
28 u32 DMA_speed1;
29 u32 GTM_flags;
32 struct ide_acpi_drive_link {
33 acpi_handle obj_handle;
34 u8 idbuff[512];
37 struct ide_acpi_hwif_link {
38 ide_hwif_t *hwif;
39 acpi_handle obj_handle;
40 struct GTM_buffer gtm;
41 struct ide_acpi_drive_link master;
42 struct ide_acpi_drive_link slave;
45 #undef DEBUGGING
46 /* note: adds function name and KERN_DEBUG */
47 #ifdef DEBUGGING
48 #define DEBPRINT(fmt, args...) \
49 printk(KERN_DEBUG "%s: " fmt, __func__, ## args)
50 #else
51 #define DEBPRINT(fmt, args...) do {} while (0)
52 #endif /* DEBUGGING */
54 static int ide_noacpi;
55 module_param_named(noacpi, ide_noacpi, bool, 0);
56 MODULE_PARM_DESC(noacpi, "disable IDE ACPI support");
58 static int ide_acpigtf;
59 module_param_named(acpigtf, ide_acpigtf, bool, 0);
60 MODULE_PARM_DESC(acpigtf, "enable IDE ACPI _GTF support");
62 static int ide_acpionboot;
63 module_param_named(acpionboot, ide_acpionboot, bool, 0);
64 MODULE_PARM_DESC(acpionboot, "call IDE ACPI methods on boot");
66 static bool ide_noacpi_psx;
67 static int no_acpi_psx(const struct dmi_system_id *id)
69 ide_noacpi_psx = true;
70 printk(KERN_NOTICE"%s detected - disable ACPI _PSx.\n", id->ident);
71 return 0;
74 static const struct dmi_system_id ide_acpi_dmi_table[] = {
75 /* Bug 9673. */
76 /* We should check if this is because ACPI NVS isn't save/restored. */
78 .callback = no_acpi_psx,
79 .ident = "HP nx9005",
80 .matches = {
81 DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies Ltd."),
82 DMI_MATCH(DMI_BIOS_VERSION, "KAM1.60")
86 { } /* terminate list */
89 int ide_acpi_init(void)
91 dmi_check_system(ide_acpi_dmi_table);
92 return 0;
95 bool ide_port_acpi(ide_hwif_t *hwif)
97 return ide_noacpi == 0 && hwif->acpidata;
101 * ide_get_dev_handle - finds acpi_handle and PCI device.function
102 * @dev: device to locate
103 * @handle: returned acpi_handle for @dev
104 * @pcidevfn: return PCI device.func for @dev
106 * Returns the ACPI object handle to the corresponding PCI device.
108 * Returns 0 on success, <0 on error.
110 static int ide_get_dev_handle(struct device *dev, acpi_handle *handle,
111 acpi_integer *pcidevfn)
113 struct pci_dev *pdev = to_pci_dev(dev);
114 unsigned int bus, devnum, func;
115 acpi_integer addr;
116 acpi_handle dev_handle;
117 acpi_status status;
118 struct acpi_device_info *dinfo = NULL;
119 int ret = -ENODEV;
121 bus = pdev->bus->number;
122 devnum = PCI_SLOT(pdev->devfn);
123 func = PCI_FUNC(pdev->devfn);
124 /* ACPI _ADR encoding for PCI bus: */
125 addr = (acpi_integer)(devnum << 16 | func);
127 DEBPRINT("ENTER: pci %02x:%02x.%01x\n", bus, devnum, func);
129 dev_handle = DEVICE_ACPI_HANDLE(dev);
130 if (!dev_handle) {
131 DEBPRINT("no acpi handle for device\n");
132 goto err;
135 status = acpi_get_object_info(dev_handle, &dinfo);
136 if (ACPI_FAILURE(status)) {
137 DEBPRINT("get_object_info for device failed\n");
138 goto err;
140 if (dinfo && (dinfo->valid & ACPI_VALID_ADR) &&
141 dinfo->address == addr) {
142 *pcidevfn = addr;
143 *handle = dev_handle;
144 } else {
145 DEBPRINT("get_object_info for device has wrong "
146 " address: %llu, should be %u\n",
147 dinfo ? (unsigned long long)dinfo->address : -1ULL,
148 (unsigned int)addr);
149 goto err;
152 DEBPRINT("for dev=0x%x.%x, addr=0x%llx, *handle=0x%p\n",
153 devnum, func, (unsigned long long)addr, *handle);
154 ret = 0;
155 err:
156 kfree(dinfo);
157 return ret;
161 * ide_acpi_hwif_get_handle - Get ACPI object handle for a given hwif
162 * @hwif: device to locate
164 * Retrieves the object handle for a given hwif.
166 * Returns handle on success, 0 on error.
168 static acpi_handle ide_acpi_hwif_get_handle(ide_hwif_t *hwif)
170 struct device *dev = hwif->gendev.parent;
171 acpi_handle uninitialized_var(dev_handle);
172 acpi_integer pcidevfn;
173 acpi_handle chan_handle;
174 int err;
176 DEBPRINT("ENTER: device %s\n", hwif->name);
178 if (!dev) {
179 DEBPRINT("no PCI device for %s\n", hwif->name);
180 return NULL;
183 err = ide_get_dev_handle(dev, &dev_handle, &pcidevfn);
184 if (err < 0) {
185 DEBPRINT("ide_get_dev_handle failed (%d)\n", err);
186 return NULL;
189 /* get child objects of dev_handle == channel objects,
190 * + _their_ children == drive objects */
191 /* channel is hwif->channel */
192 chan_handle = acpi_get_child(dev_handle, hwif->channel);
193 DEBPRINT("chan adr=%d: handle=0x%p\n",
194 hwif->channel, chan_handle);
196 return chan_handle;
200 * do_drive_get_GTF - get the drive bootup default taskfile settings
201 * @drive: the drive for which the taskfile settings should be retrieved
202 * @gtf_length: number of bytes of _GTF data returned at @gtf_address
203 * @gtf_address: buffer containing _GTF taskfile arrays
205 * The _GTF method has no input parameters.
206 * It returns a variable number of register set values (registers
207 * hex 1F1..1F7, taskfiles).
208 * The <variable number> is not known in advance, so have ACPI-CA
209 * allocate the buffer as needed and return it, then free it later.
211 * The returned @gtf_length and @gtf_address are only valid if the
212 * function return value is 0.
214 static int do_drive_get_GTF(ide_drive_t *drive,
215 unsigned int *gtf_length, unsigned long *gtf_address,
216 unsigned long *obj_loc)
218 acpi_status status;
219 struct acpi_buffer output;
220 union acpi_object *out_obj;
221 int err = -ENODEV;
223 *gtf_length = 0;
224 *gtf_address = 0UL;
225 *obj_loc = 0UL;
227 if (!drive->acpidata->obj_handle) {
228 DEBPRINT("No ACPI object found for %s\n", drive->name);
229 goto out;
232 /* Setting up output buffer */
233 output.length = ACPI_ALLOCATE_BUFFER;
234 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
236 /* _GTF has no input parameters */
237 err = -EIO;
238 status = acpi_evaluate_object(drive->acpidata->obj_handle, "_GTF",
239 NULL, &output);
240 if (ACPI_FAILURE(status)) {
241 printk(KERN_DEBUG
242 "%s: Run _GTF error: status = 0x%x\n",
243 __func__, status);
244 goto out;
247 if (!output.length || !output.pointer) {
248 DEBPRINT("Run _GTF: "
249 "length or ptr is NULL (0x%llx, 0x%p)\n",
250 (unsigned long long)output.length,
251 output.pointer);
252 goto out;
255 out_obj = output.pointer;
256 if (out_obj->type != ACPI_TYPE_BUFFER) {
257 DEBPRINT("Run _GTF: error: "
258 "expected object type of ACPI_TYPE_BUFFER, "
259 "got 0x%x\n", out_obj->type);
260 err = -ENOENT;
261 kfree(output.pointer);
262 goto out;
265 if (!out_obj->buffer.length || !out_obj->buffer.pointer ||
266 out_obj->buffer.length % REGS_PER_GTF) {
267 printk(KERN_ERR
268 "%s: unexpected GTF length (%d) or addr (0x%p)\n",
269 __func__, out_obj->buffer.length,
270 out_obj->buffer.pointer);
271 err = -ENOENT;
272 kfree(output.pointer);
273 goto out;
276 *gtf_length = out_obj->buffer.length;
277 *gtf_address = (unsigned long)out_obj->buffer.pointer;
278 *obj_loc = (unsigned long)out_obj;
279 DEBPRINT("returning gtf_length=%d, gtf_address=0x%lx, obj_loc=0x%lx\n",
280 *gtf_length, *gtf_address, *obj_loc);
281 err = 0;
282 out:
283 return err;
287 * do_drive_set_taskfiles - write the drive taskfile settings from _GTF
288 * @drive: the drive to which the taskfile command should be sent
289 * @gtf_length: total number of bytes of _GTF taskfiles
290 * @gtf_address: location of _GTF taskfile arrays
292 * Write {gtf_address, length gtf_length} in groups of
293 * REGS_PER_GTF bytes.
295 static int do_drive_set_taskfiles(ide_drive_t *drive,
296 unsigned int gtf_length,
297 unsigned long gtf_address)
299 int rc = 0, err;
300 int gtf_count = gtf_length / REGS_PER_GTF;
301 int ix;
303 DEBPRINT("total GTF bytes=%u (0x%x), gtf_count=%d, addr=0x%lx\n",
304 gtf_length, gtf_length, gtf_count, gtf_address);
306 /* send all taskfile registers (0x1f1-0x1f7) *in*that*order* */
307 for (ix = 0; ix < gtf_count; ix++) {
308 u8 *gtf = (u8 *)(gtf_address + ix * REGS_PER_GTF);
309 struct ide_cmd cmd;
311 DEBPRINT("(0x1f1-1f7): "
312 "hex: %02x %02x %02x %02x %02x %02x %02x\n",
313 gtf[0], gtf[1], gtf[2],
314 gtf[3], gtf[4], gtf[5], gtf[6]);
316 if (!ide_acpigtf) {
317 DEBPRINT("_GTF execution disabled\n");
318 continue;
321 /* convert GTF to taskfile */
322 memset(&cmd, 0, sizeof(cmd));
323 memcpy(&cmd.tf.feature, gtf, REGS_PER_GTF);
324 cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
325 cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
327 err = ide_no_data_taskfile(drive, &cmd);
328 if (err) {
329 printk(KERN_ERR "%s: ide_no_data_taskfile failed: %u\n",
330 __func__, err);
331 rc = err;
335 return rc;
339 * ide_acpi_exec_tfs - get then write drive taskfile settings
340 * @drive: the drive for which the taskfile settings should be
341 * written.
343 * According to the ACPI spec this should be called after _STM
344 * has been evaluated for the interface. Some ACPI vendors interpret
345 * that as a hard requirement and modify the taskfile according
346 * to the Identify Drive information passed down with _STM.
347 * So one should really make sure to call this only after _STM has
348 * been executed.
350 int ide_acpi_exec_tfs(ide_drive_t *drive)
352 int ret;
353 unsigned int gtf_length;
354 unsigned long gtf_address;
355 unsigned long obj_loc;
357 DEBPRINT("call get_GTF, drive=%s port=%d\n", drive->name, drive->dn);
359 ret = do_drive_get_GTF(drive, &gtf_length, &gtf_address, &obj_loc);
360 if (ret < 0) {
361 DEBPRINT("get_GTF error (%d)\n", ret);
362 return ret;
365 DEBPRINT("call set_taskfiles, drive=%s\n", drive->name);
367 ret = do_drive_set_taskfiles(drive, gtf_length, gtf_address);
368 kfree((void *)obj_loc);
369 if (ret < 0) {
370 DEBPRINT("set_taskfiles error (%d)\n", ret);
373 DEBPRINT("ret=%d\n", ret);
375 return ret;
379 * ide_acpi_get_timing - get the channel (controller) timings
380 * @hwif: target IDE interface (channel)
382 * This function executes the _GTM ACPI method for the target channel.
385 void ide_acpi_get_timing(ide_hwif_t *hwif)
387 acpi_status status;
388 struct acpi_buffer output;
389 union acpi_object *out_obj;
391 /* Setting up output buffer for _GTM */
392 output.length = ACPI_ALLOCATE_BUFFER;
393 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
395 /* _GTM has no input parameters */
396 status = acpi_evaluate_object(hwif->acpidata->obj_handle, "_GTM",
397 NULL, &output);
399 DEBPRINT("_GTM status: %d, outptr: 0x%p, outlen: 0x%llx\n",
400 status, output.pointer,
401 (unsigned long long)output.length);
403 if (ACPI_FAILURE(status)) {
404 DEBPRINT("Run _GTM error: status = 0x%x\n", status);
405 return;
408 if (!output.length || !output.pointer) {
409 DEBPRINT("Run _GTM: length or ptr is NULL (0x%llx, 0x%p)\n",
410 (unsigned long long)output.length,
411 output.pointer);
412 kfree(output.pointer);
413 return;
416 out_obj = output.pointer;
417 if (out_obj->type != ACPI_TYPE_BUFFER) {
418 kfree(output.pointer);
419 DEBPRINT("Run _GTM: error: "
420 "expected object type of ACPI_TYPE_BUFFER, "
421 "got 0x%x\n", out_obj->type);
422 return;
425 if (!out_obj->buffer.length || !out_obj->buffer.pointer ||
426 out_obj->buffer.length != sizeof(struct GTM_buffer)) {
427 kfree(output.pointer);
428 printk(KERN_ERR
429 "%s: unexpected _GTM length (0x%x)[should be 0x%zx] or "
430 "addr (0x%p)\n",
431 __func__, out_obj->buffer.length,
432 sizeof(struct GTM_buffer), out_obj->buffer.pointer);
433 return;
436 memcpy(&hwif->acpidata->gtm, out_obj->buffer.pointer,
437 sizeof(struct GTM_buffer));
439 DEBPRINT("_GTM info: ptr: 0x%p, len: 0x%x, exp.len: 0x%Zx\n",
440 out_obj->buffer.pointer, out_obj->buffer.length,
441 sizeof(struct GTM_buffer));
443 DEBPRINT("_GTM fields: 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n",
444 hwif->acpidata->gtm.PIO_speed0,
445 hwif->acpidata->gtm.DMA_speed0,
446 hwif->acpidata->gtm.PIO_speed1,
447 hwif->acpidata->gtm.DMA_speed1,
448 hwif->acpidata->gtm.GTM_flags);
450 kfree(output.pointer);
454 * ide_acpi_push_timing - set the channel (controller) timings
455 * @hwif: target IDE interface (channel)
457 * This function executes the _STM ACPI method for the target channel.
459 * _STM requires Identify Drive data, which has to passed as an argument.
460 * Unfortunately drive->id is a mangled version which we can't readily
461 * use; hence we'll get the information afresh.
463 void ide_acpi_push_timing(ide_hwif_t *hwif)
465 acpi_status status;
466 struct acpi_object_list input;
467 union acpi_object in_params[3];
468 struct ide_acpi_drive_link *master = &hwif->acpidata->master;
469 struct ide_acpi_drive_link *slave = &hwif->acpidata->slave;
471 /* Give the GTM buffer + drive Identify data to the channel via the
472 * _STM method: */
473 /* setup input parameters buffer for _STM */
474 input.count = 3;
475 input.pointer = in_params;
476 in_params[0].type = ACPI_TYPE_BUFFER;
477 in_params[0].buffer.length = sizeof(struct GTM_buffer);
478 in_params[0].buffer.pointer = (u8 *)&hwif->acpidata->gtm;
479 in_params[1].type = ACPI_TYPE_BUFFER;
480 in_params[1].buffer.length = ATA_ID_WORDS * 2;
481 in_params[1].buffer.pointer = (u8 *)&master->idbuff;
482 in_params[2].type = ACPI_TYPE_BUFFER;
483 in_params[2].buffer.length = ATA_ID_WORDS * 2;
484 in_params[2].buffer.pointer = (u8 *)&slave->idbuff;
485 /* Output buffer: _STM has no output */
487 status = acpi_evaluate_object(hwif->acpidata->obj_handle, "_STM",
488 &input, NULL);
490 if (ACPI_FAILURE(status)) {
491 DEBPRINT("Run _STM error: status = 0x%x\n", status);
493 DEBPRINT("_STM status: %d\n", status);
497 * ide_acpi_set_state - set the channel power state
498 * @hwif: target IDE interface
499 * @on: state, on/off
501 * This function executes the _PS0/_PS3 ACPI method to set the power state.
502 * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
504 void ide_acpi_set_state(ide_hwif_t *hwif, int on)
506 ide_drive_t *drive;
507 int i;
509 if (ide_noacpi_psx)
510 return;
512 DEBPRINT("ENTER:\n");
514 /* channel first and then drives for power on and verse versa for power off */
515 if (on)
516 acpi_bus_set_power(hwif->acpidata->obj_handle, ACPI_STATE_D0);
518 ide_port_for_each_present_dev(i, drive, hwif) {
519 if (drive->acpidata->obj_handle)
520 acpi_bus_set_power(drive->acpidata->obj_handle,
521 on ? ACPI_STATE_D0 : ACPI_STATE_D3);
524 if (!on)
525 acpi_bus_set_power(hwif->acpidata->obj_handle, ACPI_STATE_D3);
529 * ide_acpi_init_port - initialize the ACPI link for an IDE interface
530 * @hwif: target IDE interface (channel)
532 * The ACPI spec is not quite clear when the drive identify buffer
533 * should be obtained. Calling IDENTIFY DEVICE during shutdown
534 * is not the best of ideas as the drive might already being put to
535 * sleep. And obviously we can't call it during resume.
536 * So we get the information during startup; but this means that
537 * any changes during run-time will be lost after resume.
539 void ide_acpi_init_port(ide_hwif_t *hwif)
541 hwif->acpidata = kzalloc(sizeof(struct ide_acpi_hwif_link), GFP_KERNEL);
542 if (!hwif->acpidata)
543 return;
545 hwif->acpidata->obj_handle = ide_acpi_hwif_get_handle(hwif);
546 if (!hwif->acpidata->obj_handle) {
547 DEBPRINT("no ACPI object for %s found\n", hwif->name);
548 kfree(hwif->acpidata);
549 hwif->acpidata = NULL;
553 void ide_acpi_port_init_devices(ide_hwif_t *hwif)
555 ide_drive_t *drive;
556 int i, err;
558 if (hwif->acpidata == NULL)
559 return;
562 * The ACPI spec mandates that we send information
563 * for both drives, regardless whether they are connected
564 * or not.
566 hwif->devices[0]->acpidata = &hwif->acpidata->master;
567 hwif->devices[1]->acpidata = &hwif->acpidata->slave;
569 /* get _ADR info for each device */
570 ide_port_for_each_present_dev(i, drive, hwif) {
571 acpi_handle dev_handle;
573 DEBPRINT("ENTER: %s at channel#: %d port#: %d\n",
574 drive->name, hwif->channel, drive->dn & 1);
576 /* TBD: could also check ACPI object VALID bits */
577 dev_handle = acpi_get_child(hwif->acpidata->obj_handle,
578 drive->dn & 1);
580 DEBPRINT("drive %s handle 0x%p\n", drive->name, dev_handle);
582 drive->acpidata->obj_handle = dev_handle;
585 /* send IDENTIFY for each device */
586 ide_port_for_each_present_dev(i, drive, hwif) {
587 err = taskfile_lib_get_identify(drive, drive->acpidata->idbuff);
588 if (err)
589 DEBPRINT("identify device %s failed (%d)\n",
590 drive->name, err);
593 if (ide_noacpi || ide_acpionboot == 0) {
594 DEBPRINT("ACPI methods disabled on boot\n");
595 return;
598 /* ACPI _PS0 before _STM */
599 ide_acpi_set_state(hwif, 1);
601 * ACPI requires us to call _STM on startup
603 ide_acpi_get_timing(hwif);
604 ide_acpi_push_timing(hwif);
606 ide_port_for_each_present_dev(i, drive, hwif) {
607 ide_acpi_exec_tfs(drive);