Blame view

kernel/linux-rt-4.4.41/drivers/char/agp/isoch.c 12.9 KB
5113f6f70   김현기   kernel add
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
  /*
   * Setup routines for AGP 3.5 compliant bridges.
   */
  
  #include <linux/list.h>
  #include <linux/pci.h>
  #include <linux/agp_backend.h>
  #include <linux/module.h>
  #include <linux/slab.h>
  
  #include "agp.h"
  
  /* Generic AGP 3.5 enabling routines */
  
  struct agp_3_5_dev {
  	struct list_head list;
  	u8 capndx;
  	u32 maxbw;
  	struct pci_dev *dev;
  };
  
  static void agp_3_5_dev_list_insert(struct list_head *head, struct list_head *new)
  {
  	struct agp_3_5_dev *cur, *n = list_entry(new, struct agp_3_5_dev, list);
  	struct list_head *pos;
  
  	list_for_each(pos, head) {
  		cur = list_entry(pos, struct agp_3_5_dev, list);
  		if (cur->maxbw > n->maxbw)
  			break;
  	}
  	list_add_tail(new, pos);
  }
  
  static void agp_3_5_dev_list_sort(struct agp_3_5_dev *list, unsigned int ndevs)
  {
  	struct agp_3_5_dev *cur;
  	struct pci_dev *dev;
  	struct list_head *pos, *tmp, *head = &list->list, *start = head->next;
  	u32 nistat;
  
  	INIT_LIST_HEAD(head);
  
  	for (pos=start; pos!=head; ) {
  		cur = list_entry(pos, struct agp_3_5_dev, list);
  		dev = cur->dev;
  
  		pci_read_config_dword(dev, cur->capndx+AGPNISTAT, &nistat);
  		cur->maxbw = (nistat >> 16) & 0xff;
  
  		tmp = pos;
  		pos = pos->next;
  		agp_3_5_dev_list_insert(head, tmp);
  	}
  }
  
  /*
   * Initialize all isochronous transfer parameters for an AGP 3.0
   * node (i.e. a host bridge in combination with the adapters
   * lying behind it...)
   */
  
  static int agp_3_5_isochronous_node_enable(struct agp_bridge_data *bridge,
  		struct agp_3_5_dev *dev_list, unsigned int ndevs)
  {
  	/*
  	 * Convenience structure to make the calculations clearer
  	 * here.  The field names come straight from the AGP 3.0 spec.
  	 */
  	struct isoch_data {
  		u32 maxbw;
  		u32 n;
  		u32 y;
  		u32 l;
  		u32 rq;
  		struct agp_3_5_dev *dev;
  	};
  
  	struct pci_dev *td = bridge->dev, *dev;
  	struct list_head *head = &dev_list->list, *pos;
  	struct agp_3_5_dev *cur;
  	struct isoch_data *master, target;
  	unsigned int cdev = 0;
  	u32 mnistat, tnistat, tstatus, mcmd;
  	u16 tnicmd, mnicmd;
  	u8 mcapndx;
  	u32 tot_bw = 0, tot_n = 0, tot_rq = 0, y_max, rq_isoch, rq_async;
  	u32 step, rem, rem_isoch, rem_async;
  	int ret = 0;
  
  	/*
  	 * We'll work with an array of isoch_data's (one for each
  	 * device in dev_list) throughout this function.
  	 */
  	if ((master = kmalloc(ndevs * sizeof(*master), GFP_KERNEL)) == NULL) {
  		ret = -ENOMEM;
  		goto get_out;
  	}
  
  	/*
  	 * Sort the device list by maxbw.  We need to do this because the
  	 * spec suggests that the devices with the smallest requirements
  	 * have their resources allocated first, with all remaining resources
  	 * falling to the device with the largest requirement.
  	 *
  	 * We don't exactly do this, we divide target resources by ndevs
  	 * and split them amongst the AGP 3.0 devices.  The remainder of such
  	 * division operations are dropped on the last device, sort of like
  	 * the spec mentions it should be done.
  	 *
  	 * We can't do this sort when we initially construct the dev_list
  	 * because we don't know until this function whether isochronous
  	 * transfers are enabled and consequently whether maxbw will mean
  	 * anything.
  	 */
  	agp_3_5_dev_list_sort(dev_list, ndevs);
  
  	pci_read_config_dword(td, bridge->capndx+AGPNISTAT, &tnistat);
  	pci_read_config_dword(td, bridge->capndx+AGPSTAT, &tstatus);
  
  	/* Extract power-on defaults from the target */
  	target.maxbw = (tnistat >> 16) & 0xff;
  	target.n     = (tnistat >> 8)  & 0xff;
  	target.y     = (tnistat >> 6)  & 0x3;
  	target.l     = (tnistat >> 3)  & 0x7;
  	target.rq    = (tstatus >> 24) & 0xff;
  
  	y_max = target.y;
  
  	/*
  	 * Extract power-on defaults for each device in dev_list.  Along
  	 * the way, calculate the total isochronous bandwidth required
  	 * by these devices and the largest requested payload size.
  	 */
  	list_for_each(pos, head) {
  		cur = list_entry(pos, struct agp_3_5_dev, list);
  		dev = cur->dev;
  
  		mcapndx = cur->capndx;
  
  		pci_read_config_dword(dev, cur->capndx+AGPNISTAT, &mnistat);
  
  		master[cdev].maxbw = (mnistat >> 16) & 0xff;
  		master[cdev].n     = (mnistat >> 8)  & 0xff;
  		master[cdev].y     = (mnistat >> 6)  & 0x3;
  		master[cdev].dev   = cur;
  
  		tot_bw += master[cdev].maxbw;
  		y_max = max(y_max, master[cdev].y);
  
  		cdev++;
  	}
  
  	/* Check if this configuration has any chance of working */
  	if (tot_bw > target.maxbw) {
  		dev_err(&td->dev, "isochronous bandwidth required "
  			"by AGP 3.0 devices exceeds that which is supported by "
  			"the AGP 3.0 bridge!
  ");
  		ret = -ENODEV;
  		goto free_and_exit;
  	}
  
  	target.y = y_max;
  
  	/*
  	 * Write the calculated payload size into the target's NICMD
  	 * register.  Doing this directly effects the ISOCH_N value
  	 * in the target's NISTAT register, so we need to do this now
  	 * to get an accurate value for ISOCH_N later.
  	 */
  	pci_read_config_word(td, bridge->capndx+AGPNICMD, &tnicmd);
  	tnicmd &= ~(0x3 << 6);
  	tnicmd |= target.y << 6;
  	pci_write_config_word(td, bridge->capndx+AGPNICMD, tnicmd);
  
  	/* Reread the target's ISOCH_N */
  	pci_read_config_dword(td, bridge->capndx+AGPNISTAT, &tnistat);
  	target.n = (tnistat >> 8) & 0xff;
  
  	/* Calculate the minimum ISOCH_N needed by each master */
  	for (cdev=0; cdev<ndevs; cdev++) {
  		master[cdev].y = target.y;
  		master[cdev].n = master[cdev].maxbw / (master[cdev].y + 1);
  
  		tot_n += master[cdev].n;
  	}
  
  	/* Exit if the minimal ISOCH_N allocation among the masters is more
  	 * than the target can handle. */
  	if (tot_n > target.n) {
  		dev_err(&td->dev, "number of isochronous "
  			"transactions per period required by AGP 3.0 devices "
  			"exceeds that which is supported by the AGP 3.0 "
  			"bridge!
  ");
  		ret = -ENODEV;
  		goto free_and_exit;
  	}
  
  	/* Calculate left over ISOCH_N capability in the target.  We'll give
  	 * this to the hungriest device (as per the spec) */
  	rem  = target.n - tot_n;
  
  	/*
  	 * Calculate the minimum isochronous RQ depth needed by each master.
  	 * Along the way, distribute the extra ISOCH_N capability calculated
  	 * above.
  	 */
  	for (cdev=0; cdev<ndevs; cdev++) {
  		/*
  		 * This is a little subtle.  If ISOCH_Y > 64B, then ISOCH_Y
  		 * byte isochronous writes will be broken into 64B pieces.
  		 * This means we need to budget more RQ depth to account for
  		 * these kind of writes (each isochronous write is actually
  		 * many writes on the AGP bus).
  		 */
  		master[cdev].rq = master[cdev].n;
  		if (master[cdev].y > 0x1)
  			master[cdev].rq *= (1 << (master[cdev].y - 1));
  
  		tot_rq += master[cdev].rq;
  	}
  	master[ndevs-1].n += rem;
  
  	/* Figure the number of isochronous and asynchronous RQ slots the
  	 * target is providing. */
  	rq_isoch = (target.y > 0x1) ? target.n * (1 << (target.y - 1)) : target.n;
  	rq_async = target.rq - rq_isoch;
  
  	/* Exit if the minimal RQ needs of the masters exceeds what the target
  	 * can provide. */
  	if (tot_rq > rq_isoch) {
  		dev_err(&td->dev, "number of request queue slots "
  			"required by the isochronous bandwidth requested by "
  			"AGP 3.0 devices exceeds the number provided by the "
  			"AGP 3.0 bridge!
  ");
  		ret = -ENODEV;
  		goto free_and_exit;
  	}
  
  	/* Calculate asynchronous RQ capability in the target (per master) as
  	 * well as the total number of leftover isochronous RQ slots. */
  	step      = rq_async / ndevs;
  	rem_async = step + (rq_async % ndevs);
  	rem_isoch = rq_isoch - tot_rq;
  
  	/* Distribute the extra RQ slots calculated above and write our
  	 * isochronous settings out to the actual devices. */
  	for (cdev=0; cdev<ndevs; cdev++) {
  		cur = master[cdev].dev;
  		dev = cur->dev;
  
  		mcapndx = cur->capndx;
  
  		master[cdev].rq += (cdev == ndevs - 1)
  		              ? (rem_async + rem_isoch) : step;
  
  		pci_read_config_word(dev, cur->capndx+AGPNICMD, &mnicmd);
  		pci_read_config_dword(dev, cur->capndx+AGPCMD, &mcmd);
  
  		mnicmd &= ~(0xff << 8);
  		mnicmd &= ~(0x3  << 6);
  		mcmd   &= ~(0xff << 24);
  
  		mnicmd |= master[cdev].n  << 8;
  		mnicmd |= master[cdev].y  << 6;
  		mcmd   |= master[cdev].rq << 24;
  
  		pci_write_config_dword(dev, cur->capndx+AGPCMD, mcmd);
  		pci_write_config_word(dev, cur->capndx+AGPNICMD, mnicmd);
  	}
  
  free_and_exit:
  	kfree(master);
  
  get_out:
  	return ret;
  }
  
  /*
   * This function basically allocates request queue slots among the
   * AGP 3.0 systems in nonisochronous nodes.  The algorithm is
   * pretty stupid, divide the total number of RQ slots provided by the
   * target by ndevs.  Distribute this many slots to each AGP 3.0 device,
   * giving any left over slots to the last device in dev_list.
   */
  static void agp_3_5_nonisochronous_node_enable(struct agp_bridge_data *bridge,
  		struct agp_3_5_dev *dev_list, unsigned int ndevs)
  {
  	struct agp_3_5_dev *cur;
  	struct list_head *head = &dev_list->list, *pos;
  	u32 tstatus, mcmd;
  	u32 trq, mrq, rem;
  	unsigned int cdev = 0;
  
  	pci_read_config_dword(bridge->dev, bridge->capndx+AGPSTAT, &tstatus);
  
  	trq = (tstatus >> 24) & 0xff;
  	mrq = trq / ndevs;
  
  	rem = mrq + (trq % ndevs);
  
  	for (pos=head->next; cdev<ndevs; cdev++, pos=pos->next) {
  		cur = list_entry(pos, struct agp_3_5_dev, list);
  
  		pci_read_config_dword(cur->dev, cur->capndx+AGPCMD, &mcmd);
  		mcmd &= ~(0xff << 24);
  		mcmd |= ((cdev == ndevs - 1) ? rem : mrq) << 24;
  		pci_write_config_dword(cur->dev, cur->capndx+AGPCMD, mcmd);
  	}
  }
  
  /*
   * Fully configure and enable an AGP 3.0 host bridge and all the devices
   * lying behind it.
   */
  int agp_3_5_enable(struct agp_bridge_data *bridge)
  {
  	struct pci_dev *td = bridge->dev, *dev = NULL;
  	u8 mcapndx;
  	u32 isoch, arqsz;
  	u32 tstatus, mstatus, ncapid;
  	u32 mmajor;
  	u16 mpstat;
  	struct agp_3_5_dev *dev_list, *cur;
  	struct list_head *head, *pos;
  	unsigned int ndevs = 0;
  	int ret = 0;
  
  	/* Extract some power-on defaults from the target */
  	pci_read_config_dword(td, bridge->capndx+AGPSTAT, &tstatus);
  	isoch     = (tstatus >> 17) & 0x1;
  	if (isoch == 0)	/* isoch xfers not available, bail out. */
  		return -ENODEV;
  
  	arqsz     = (tstatus >> 13) & 0x7;
  
  	/*
  	 * Allocate a head for our AGP 3.5 device list
  	 * (multiple AGP v3 devices are allowed behind a single bridge).
  	 */
  	if ((dev_list = kmalloc(sizeof(*dev_list), GFP_KERNEL)) == NULL) {
  		ret = -ENOMEM;
  		goto get_out;
  	}
  	head = &dev_list->list;
  	INIT_LIST_HEAD(head);
  
  	/* Find all AGP devices, and add them to dev_list. */
  	for_each_pci_dev(dev) {
  		mcapndx = pci_find_capability(dev, PCI_CAP_ID_AGP);
  		if (mcapndx == 0)
  			continue;
  
  		switch ((dev->class >>8) & 0xff00) {
  			case 0x0600:    /* Bridge */
  				/* Skip bridges. We should call this function for each one. */
  				continue;
  
  			case 0x0001:    /* Unclassified device */
  				/* Don't know what this is, but log it for investigation. */
  				if (mcapndx != 0) {
  					dev_info(&td->dev, "wacky, found unclassified AGP device %s [%04x/%04x]
  ",
  						 pci_name(dev),
  						 dev->vendor, dev->device);
  				}
  				continue;
  
  			case 0x0300:    /* Display controller */
  			case 0x0400:    /* Multimedia controller */
  				if ((cur = kmalloc(sizeof(*cur), GFP_KERNEL)) == NULL) {
  					ret = -ENOMEM;
  					goto free_and_exit;
  				}
  				cur->dev = dev;
  
  				pos = &cur->list;
  				list_add(pos, head);
  				ndevs++;
  				continue;
  
  			default:
  				continue;
  		}
  	}
  
  	/*
  	 * Take an initial pass through the devices lying behind our host
  	 * bridge.  Make sure each one is actually an AGP 3.0 device, otherwise
  	 * exit with an error message.  Along the way store the AGP 3.0
  	 * cap_ptr for each device
  	 */
  	list_for_each(pos, head) {
  		cur = list_entry(pos, struct agp_3_5_dev, list);
  		dev = cur->dev;
  
  		pci_read_config_word(dev, PCI_STATUS, &mpstat);
  		if ((mpstat & PCI_STATUS_CAP_LIST) == 0)
  			continue;
  
  		pci_read_config_byte(dev, PCI_CAPABILITY_LIST, &mcapndx);
  		if (mcapndx != 0) {
  			do {
  				pci_read_config_dword(dev, mcapndx, &ncapid);
  				if ((ncapid & 0xff) != 2)
  					mcapndx = (ncapid >> 8) & 0xff;
  			}
  			while (((ncapid & 0xff) != 2) && (mcapndx != 0));
  		}
  
  		if (mcapndx == 0) {
  			dev_err(&td->dev, "woah!  Non-AGP device %s on "
  				"secondary bus of AGP 3.5 bridge!
  ",
  				pci_name(dev));
  			ret = -ENODEV;
  			goto free_and_exit;
  		}
  
  		mmajor = (ncapid >> AGP_MAJOR_VERSION_SHIFT) & 0xf;
  		if (mmajor < 3) {
  			dev_err(&td->dev, "woah!  AGP 2.0 device %s on "
  				"secondary bus of AGP 3.5 bridge operating "
  				"with AGP 3.0 electricals!
  ", pci_name(dev));
  			ret = -ENODEV;
  			goto free_and_exit;
  		}
  
  		cur->capndx = mcapndx;
  
  		pci_read_config_dword(dev, cur->capndx+AGPSTAT, &mstatus);
  
  		if (((mstatus >> 3) & 0x1) == 0) {
  			dev_err(&td->dev, "woah!  AGP 3.x device %s not "
  				"operating in AGP 3.x mode on secondary bus "
  				"of AGP 3.5 bridge operating with AGP 3.0 "
  				"electricals!
  ", pci_name(dev));
  			ret = -ENODEV;
  			goto free_and_exit;
  		}
  	}		
  
  	/*
  	 * Call functions to divide target resources amongst the AGP 3.0
  	 * masters.  This process is dramatically different depending on
  	 * whether isochronous transfers are supported.
  	 */
  	if (isoch) {
  		ret = agp_3_5_isochronous_node_enable(bridge, dev_list, ndevs);
  		if (ret) {
  			dev_info(&td->dev, "something bad happened setting "
  				 "up isochronous xfers; falling back to "
  				 "non-isochronous xfer mode
  ");
  		} else {
  			goto free_and_exit;
  		}
  	}
  	agp_3_5_nonisochronous_node_enable(bridge, dev_list, ndevs);
  
  free_and_exit:
  	/* Be sure to free the dev_list */
  	for (pos=head->next; pos!=head; ) {
  		cur = list_entry(pos, struct agp_3_5_dev, list);
  
  		pos = pos->next;
  		kfree(cur);
  	}
  	kfree(dev_list);
  
  get_out:
  	return ret;
  }