aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/emgd/emgd/utils/pci.c
blob: 576485322b2c8c4ebe8997004773384aeb357b78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* -*- pse-c -*-
 *-----------------------------------------------------------------------------
 * Filename: pci.c
 * $Revision: 1.3 $
 *-----------------------------------------------------------------------------
 * Copyright © 2002-2010, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *-----------------------------------------------------------------------------
 * Description:
 *  This file contains Linux kernel space PCI API for the OAL abstractions.
 *-----------------------------------------------------------------------------
 */

#include <memory.h>
#include <pci.h>
#include <linux/pci.h>
#include <io.h>

#if defined(CONFIG_VGA_ARB)
#include <linux/vgaarb.h>
#endif


//
// This is our local representation of a PCI device.
// This struct is private to this file, and exposed to calling functions
// only through an opaque "os_pci_dev_t" handle.
//
typedef struct _linuxkernel_pci {
  struct pci_dev *dev;
  unsigned int bus;
  unsigned int slot;
  unsigned int func;
}linuxkernel_pci_t;

/*----------------------------------------------------------------------
 * Function:
 *pci_find_device_generic
 * Parameters:
 *unsigned short vendor,
 *unsigned short device,
 *os_pci_dev_t os_pci_dev
 * Description:
 *This function finds a PCI device by going through 255 buses, 32 devices
 *and 8 functions and tries to match each ones vendor and device ids with
 *the ones given to it as parameters. Stops for the first device it finds 
 *with a matching vendor and device, so it will not find multiple devices.
 *This is _NOT_ an exported OAL
 *function.
 * Returns:
 *os_pci_dev_t
 *---------------------------------------------------------------------
 */
static os_pci_dev_t pci_find_device_generic(
					    unsigned short vendor_id,
					    unsigned short device_id,
					    os_pci_dev_t pci_dev_handle
					    )
{
  struct pci_dev *our_device; // Kernel struct for a PCI device
  linuxkernel_pci_t *pdev; // Our struct for a PCI device. 

    // Locate the device, and lock it. Start search at the start of the list.
    our_device = pci_get_device(vendor_id, device_id, NULL);
    // If we didn't find it, return an error.
    if (!our_device){
      return (os_pci_dev_t)NULL;
    }

    // Get the pointer to the destination for the data.  
    // pci_dev_handle is an opaque pointer to a linuxkernel_pci_t struct
    // If there isn't one, allocate it.
    pdev = (linuxkernel_pci_t *)pci_dev_handle;
    if(!pdev) {

      // Caller did not supply a handle to a PCI device struct.  
      // Allocate one.
      pdev = (linuxkernel_pci_t *)OS_ALLOC(sizeof(linuxkernel_pci_t));
      if(!pdev) {
	return (os_pci_dev_t)NULL;
      }
      // Zero the destination memory
      memset(pdev, 0, sizeof(linuxkernel_pci_t));
    }

    // Copy over from the kernel struct to our struct.
    // It is safe to copy a pointer to the pci_dev, since we 
    // have a lock on it.
    pdev->dev = our_device;
    pdev->bus = our_device->bus->number;
    pdev->slot = PCI_SLOT(our_device->devfn);
    pdev->func = PCI_FUNC(our_device->devfn);

    return (os_pci_dev_t)pdev;
}


/*----------------------------------------------------------------------
 * Function:
 *os_pci_find_device
 *
 *  Parameters:
 *unsigned short vendor,
 *unsigned short device,
 *unsigned short bus,
 *unsigned short dev,
 *unsigned short func,
 *os_pci_dev_t os_pci_dev
 *
 *  Description:
 *  This function finds a PCI device by scanning the specified bus, dev, func
 *and tries to match vendor and device ids with the ones given to it as
 *parameters.
 *
 *  Notes: If the bus number is 0xFFFF, then the function searches for that
 *  vendor_id, device_id pair in the whole PCI topology of the system i.e
 *  it goes through all the buses, devices, functions in the system
 *
 * Returns:
 *os_pci_dev_t
 *---------------------------------------------------------------------
 */
os_pci_dev_t os_pci_find_device(
				unsigned short vendor_id,
				unsigned short device_id,
				unsigned short bus,
				unsigned short dev,
				unsigned short func,
				os_pci_dev_t pci_dev)
{
  /* TODO: Right now, Just fallback to pci_find_device_generic
   * But we need to implement this
   */
  return pci_find_device_generic(vendor_id, device_id, pci_dev);
}

int os_pci_get_slot_address(
			    os_pci_dev_t pci_dev,
			    unsigned int *bus,
			    unsigned int *slot,
			    unsigned int *func)
{

  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
  EMGD_ASSERT(pdev, "Invalid pci device", 0);

  if(bus) {
    *bus = pdev->bus;
  }
  if(slot) {
    *slot = pdev->slot;
  }
  if(func) {
    *func = pdev->func;
  }
  return 0;
}

int os_pci_read_config_8(
			 os_pci_dev_t pci_dev,
			 unsigned long offset,
			 unsigned char* val
			 )
{
  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
  EMGD_ASSERT(pdev, "Invalid pci device", 0);
  EMGD_ASSERT(val, "Invalid pointer", 0);
  return pci_read_config_byte(pdev->dev, offset, val);
}

int os_pci_read_config_16(
			  os_pci_dev_t pci_dev,
			  unsigned long offset,
			  unsigned short* val
			  )
{
  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
  EMGD_ASSERT(pdev, "Invalid pci device", 0);
  EMGD_ASSERT(val, "Invalid pointer", 0);
  return pci_read_config_word(pdev->dev, offset,val);
}

int os_pci_read_config_32(
			  os_pci_dev_t pci_dev,
			  unsigned long offset,
			  unsigned long* val
			  )
{
  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
  EMGD_ASSERT(pdev, "Invalid pci device", 0);
  EMGD_ASSERT(val, "Invalid pointer", 0);
  return pci_read_config_dword(pdev->dev, offset, (u32*)val);
}

int os_pci_write_config_8(
			  os_pci_dev_t pci_dev,
			  unsigned long offset,
			  unsigned char val
			  )
{
  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
  EMGD_ASSERT(pdev, "Invalid pci device", 0);
  return pci_write_config_byte(pdev->dev, offset, val);
}

int os_pci_write_config_16(
			   os_pci_dev_t pci_dev,
			   unsigned long offset,
			   unsigned short val
			   )
{
  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
  EMGD_ASSERT(pdev, "Invalid pci device", 0);
  return pci_write_config_word(pdev->dev, offset, val);
}

int os_pci_write_config_32(
			   os_pci_dev_t pci_dev,
			   unsigned long offset,
			   unsigned long val
			   )
{
  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
  EMGD_ASSERT(pdev, "Invalid pci device", 0);
  return pci_write_config_dword(pdev->dev, offset, val);
}

int os_pci_disable_legacy_vga_decoding(
			os_pci_dev_t pci_dev
		)
{
#if defined(CONFIG_VGA_ARB)
	linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
	EMGD_ASSERT(pdev, "Invalid pci device", 0);

	vga_set_legacy_decoding(pdev->dev, VGA_RSRC_NONE);
#else
	/* Noop if the VGA arbiter isn't compiled into the kernel */
#endif

	return 0;
}


void os_pci_free_device(
			os_pci_dev_t pci_dev
			)
{
  linuxkernel_pci_t *pdev = (linuxkernel_pci_t *)pci_dev;
  
  // Release the lock on our PCI device struct
  pci_dev_put(pdev->dev);

  // Free our local structure.  
  OS_FREE(pdev);

  return;
}