xref: /OK3568_Linux_fs/kernel/drivers/gpu/arm/midgard/mali_kbase_10969_workaround.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  *
3  * (C) COPYRIGHT 2013-2015, 2017 ARM Limited. All rights reserved.
4  *
5  * This program is free software and is provided to you under the terms of the
6  * GNU General Public License version 2 as published by the Free Software
7  * Foundation, and any use by you of this program is subject to the terms
8  * of such GNU licence.
9  *
10  * A copy of the licence is included with the program, and can also be obtained
11  * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
12  * Boston, MA  02110-1301, USA.
13  *
14  */
15 
16 
17 #include <linux/dma-mapping.h>
18 #include <mali_kbase.h>
19 #include <mali_kbase_10969_workaround.h>
20 
21 /* This function is used to solve an HW issue with single iterator GPUs.
22  * If a fragment job is soft-stopped on the edge of its bounding box, can happen that the
23  * restart index is out of bounds and the rerun causes a tile range fault. If this happens
24  * we try to clamp the restart index to a correct value and rerun the job.
25  */
26 /* Mask of X and Y coordinates for the coordinates words in the descriptors*/
27 #define X_COORDINATE_MASK 0x00000FFF
28 #define Y_COORDINATE_MASK 0x0FFF0000
29 /* Max number of words needed from the fragment shader job descriptor */
30 #define JOB_HEADER_SIZE_IN_WORDS 10
31 #define JOB_HEADER_SIZE (JOB_HEADER_SIZE_IN_WORDS*sizeof(u32))
32 
33 /* Word 0: Status Word */
34 #define JOB_DESC_STATUS_WORD 0
35 /* Word 1: Restart Index */
36 #define JOB_DESC_RESTART_INDEX_WORD 1
37 /* Word 2: Fault address low word */
38 #define JOB_DESC_FAULT_ADDR_LOW_WORD 2
39 /* Word 8: Minimum Tile Coordinates */
40 #define FRAG_JOB_DESC_MIN_TILE_COORD_WORD 8
41 /* Word 9: Maximum Tile Coordinates */
42 #define FRAG_JOB_DESC_MAX_TILE_COORD_WORD 9
43 
kbasep_10969_workaround_clamp_coordinates(struct kbase_jd_atom * katom)44 int kbasep_10969_workaround_clamp_coordinates(struct kbase_jd_atom *katom)
45 {
46 	struct device *dev = katom->kctx->kbdev->dev;
47 	u32   clamped = 0;
48 	struct kbase_va_region *region;
49 	phys_addr_t *page_array;
50 	u64 page_index;
51 	u32 offset = katom->jc & (~PAGE_MASK);
52 	u32 *page_1 = NULL;
53 	u32 *page_2 = NULL;
54 	u32   job_header[JOB_HEADER_SIZE_IN_WORDS];
55 	void *dst = job_header;
56 	u32 minX, minY, maxX, maxY;
57 	u32 restartX, restartY;
58 	struct page *p;
59 	u32 copy_size;
60 
61 	dev_warn(dev, "Called TILE_RANGE_FAULT workaround clamping function.\n");
62 	if (!(katom->core_req & BASE_JD_REQ_FS))
63 		return 0;
64 
65 	kbase_gpu_vm_lock(katom->kctx);
66 	region = kbase_region_tracker_find_region_enclosing_address(katom->kctx,
67 			katom->jc);
68 	if (!region || (region->flags & KBASE_REG_FREE))
69 		goto out_unlock;
70 
71 	page_array = kbase_get_cpu_phy_pages(region);
72 	if (!page_array)
73 		goto out_unlock;
74 
75 	page_index = (katom->jc >> PAGE_SHIFT) - region->start_pfn;
76 
77 	p = pfn_to_page(PFN_DOWN(page_array[page_index]));
78 
79 	/* we need the first 10 words of the fragment shader job descriptor.
80 	 * We need to check that the offset + 10 words is less that the page
81 	 * size otherwise we need to load the next page.
82 	 * page_size_overflow will be equal to 0 in case the whole descriptor
83 	 * is within the page > 0 otherwise.
84 	 */
85 	copy_size = MIN(PAGE_SIZE - offset, JOB_HEADER_SIZE);
86 
87 	page_1 = kmap_atomic(p);
88 
89 	/* page_1 is a u32 pointer, offset is expressed in bytes */
90 	page_1 += offset>>2;
91 
92 	kbase_sync_single_for_cpu(katom->kctx->kbdev,
93 			kbase_dma_addr(p) + offset,
94 			copy_size, DMA_BIDIRECTIONAL);
95 
96 	memcpy(dst, page_1, copy_size);
97 
98 	/* The data needed overflows page the dimension,
99 	 * need to map the subsequent page */
100 	if (copy_size < JOB_HEADER_SIZE) {
101 		p = pfn_to_page(PFN_DOWN(page_array[page_index + 1]));
102 		page_2 = kmap_atomic(p);
103 
104 		kbase_sync_single_for_cpu(katom->kctx->kbdev,
105 				kbase_dma_addr(p),
106 				JOB_HEADER_SIZE - copy_size, DMA_BIDIRECTIONAL);
107 
108 		memcpy(dst + copy_size, page_2, JOB_HEADER_SIZE - copy_size);
109 	}
110 
111 	/* We managed to correctly map one or two pages (in case of overflow) */
112 	/* Get Bounding Box data and restart index from fault address low word */
113 	minX = job_header[FRAG_JOB_DESC_MIN_TILE_COORD_WORD] & X_COORDINATE_MASK;
114 	minY = job_header[FRAG_JOB_DESC_MIN_TILE_COORD_WORD] & Y_COORDINATE_MASK;
115 	maxX = job_header[FRAG_JOB_DESC_MAX_TILE_COORD_WORD] & X_COORDINATE_MASK;
116 	maxY = job_header[FRAG_JOB_DESC_MAX_TILE_COORD_WORD] & Y_COORDINATE_MASK;
117 	restartX = job_header[JOB_DESC_FAULT_ADDR_LOW_WORD] & X_COORDINATE_MASK;
118 	restartY = job_header[JOB_DESC_FAULT_ADDR_LOW_WORD] & Y_COORDINATE_MASK;
119 
120 	dev_warn(dev, "Before Clamping:\n"
121 			"Jobstatus: %08x\n"
122 			"restartIdx: %08x\n"
123 			"Fault_addr_low: %08x\n"
124 			"minCoordsX: %08x minCoordsY: %08x\n"
125 			"maxCoordsX: %08x maxCoordsY: %08x\n",
126 			job_header[JOB_DESC_STATUS_WORD],
127 			job_header[JOB_DESC_RESTART_INDEX_WORD],
128 			job_header[JOB_DESC_FAULT_ADDR_LOW_WORD],
129 			minX, minY,
130 			maxX, maxY);
131 
132 	/* Set the restart index to the one which generated the fault*/
133 	job_header[JOB_DESC_RESTART_INDEX_WORD] =
134 			job_header[JOB_DESC_FAULT_ADDR_LOW_WORD];
135 
136 	if (restartX < minX) {
137 		job_header[JOB_DESC_RESTART_INDEX_WORD] = (minX) | restartY;
138 		dev_warn(dev,
139 			"Clamping restart X index to minimum. %08x clamped to %08x\n",
140 			restartX, minX);
141 		clamped =  1;
142 	}
143 	if (restartY < minY) {
144 		job_header[JOB_DESC_RESTART_INDEX_WORD] = (minY) | restartX;
145 		dev_warn(dev,
146 			"Clamping restart Y index to minimum. %08x clamped to %08x\n",
147 			restartY, minY);
148 		clamped =  1;
149 	}
150 	if (restartX > maxX) {
151 		job_header[JOB_DESC_RESTART_INDEX_WORD] = (maxX) | restartY;
152 		dev_warn(dev,
153 			"Clamping restart X index to maximum. %08x clamped to %08x\n",
154 			restartX, maxX);
155 		clamped =  1;
156 	}
157 	if (restartY > maxY) {
158 		job_header[JOB_DESC_RESTART_INDEX_WORD] = (maxY) | restartX;
159 		dev_warn(dev,
160 			"Clamping restart Y index to maximum. %08x clamped to %08x\n",
161 			restartY, maxY);
162 		clamped =  1;
163 	}
164 
165 	if (clamped) {
166 		/* Reset the fault address low word
167 		 * and set the job status to STOPPED */
168 		job_header[JOB_DESC_FAULT_ADDR_LOW_WORD] = 0x0;
169 		job_header[JOB_DESC_STATUS_WORD] = BASE_JD_EVENT_STOPPED;
170 		dev_warn(dev, "After Clamping:\n"
171 				"Jobstatus: %08x\n"
172 				"restartIdx: %08x\n"
173 				"Fault_addr_low: %08x\n"
174 				"minCoordsX: %08x minCoordsY: %08x\n"
175 				"maxCoordsX: %08x maxCoordsY: %08x\n",
176 				job_header[JOB_DESC_STATUS_WORD],
177 				job_header[JOB_DESC_RESTART_INDEX_WORD],
178 				job_header[JOB_DESC_FAULT_ADDR_LOW_WORD],
179 				minX, minY,
180 				maxX, maxY);
181 
182 		/* Flush CPU cache to update memory for future GPU reads*/
183 		memcpy(page_1, dst, copy_size);
184 		p = pfn_to_page(PFN_DOWN(page_array[page_index]));
185 
186 		kbase_sync_single_for_device(katom->kctx->kbdev,
187 				kbase_dma_addr(p) + offset,
188 				copy_size, DMA_TO_DEVICE);
189 
190 		if (copy_size < JOB_HEADER_SIZE) {
191 			memcpy(page_2, dst + copy_size,
192 					JOB_HEADER_SIZE - copy_size);
193 			p = pfn_to_page(PFN_DOWN(page_array[page_index + 1]));
194 
195 			kbase_sync_single_for_device(katom->kctx->kbdev,
196 					kbase_dma_addr(p),
197 					JOB_HEADER_SIZE - copy_size,
198 					DMA_TO_DEVICE);
199 		}
200 	}
201 	if (copy_size < JOB_HEADER_SIZE)
202 		kunmap_atomic(page_2);
203 
204 	kunmap_atomic(page_1);
205 
206 out_unlock:
207 	kbase_gpu_vm_unlock(katom->kctx);
208 	return clamped;
209 }
210