i915_gem.c 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633
  1. /*
  2. * Copyright © 2008 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/i915_drm.h>
  29. #include "i915_drv.h"
  30. #include "i915_trace.h"
  31. #include "intel_drv.h"
  32. #include <linux/shmem_fs.h>
  33. #include <linux/slab.h>
  34. #include <linux/swap.h>
  35. #include <linux/pci.h>
  36. #include <linux/dma-buf.h>
  37. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  38. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
  39. static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
  40. unsigned alignment,
  41. bool map_and_fenceable,
  42. bool nonblocking);
  43. static int i915_gem_phys_pwrite(struct drm_device *dev,
  44. struct drm_i915_gem_object *obj,
  45. struct drm_i915_gem_pwrite *args,
  46. struct drm_file *file);
  47. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  48. struct drm_i915_gem_object *obj);
  49. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  50. struct drm_i915_fence_reg *fence,
  51. bool enable);
  52. static int i915_gem_inactive_shrink(struct shrinker *shrinker,
  53. struct shrink_control *sc);
  54. static long i915_gem_purge(struct drm_i915_private *dev_priv, long target);
  55. static void i915_gem_shrink_all(struct drm_i915_private *dev_priv);
  56. static void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
  57. static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
  58. {
  59. if (obj->tiling_mode)
  60. i915_gem_release_mmap(obj);
  61. /* As we do not have an associated fence register, we will force
  62. * a tiling change if we ever need to acquire one.
  63. */
  64. obj->fence_dirty = false;
  65. obj->fence_reg = I915_FENCE_REG_NONE;
  66. }
  67. /* some bookkeeping */
  68. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  69. size_t size)
  70. {
  71. dev_priv->mm.object_count++;
  72. dev_priv->mm.object_memory += size;
  73. }
  74. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  75. size_t size)
  76. {
  77. dev_priv->mm.object_count--;
  78. dev_priv->mm.object_memory -= size;
  79. }
  80. static int
  81. i915_gem_wait_for_error(struct i915_gpu_error *error)
  82. {
  83. int ret;
  84. #define EXIT_COND (!i915_reset_in_progress(error) || \
  85. i915_terminally_wedged(error))
  86. if (EXIT_COND)
  87. return 0;
  88. /*
  89. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  90. * userspace. If it takes that long something really bad is going on and
  91. * we should simply try to bail out and fail as gracefully as possible.
  92. */
  93. ret = wait_event_interruptible_timeout(error->reset_queue,
  94. EXIT_COND,
  95. 10*HZ);
  96. if (ret == 0) {
  97. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  98. return -EIO;
  99. } else if (ret < 0) {
  100. return ret;
  101. }
  102. #undef EXIT_COND
  103. return 0;
  104. }
  105. int i915_mutex_lock_interruptible(struct drm_device *dev)
  106. {
  107. struct drm_i915_private *dev_priv = dev->dev_private;
  108. int ret;
  109. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  110. if (ret)
  111. return ret;
  112. ret = mutex_lock_interruptible(&dev->struct_mutex);
  113. if (ret)
  114. return ret;
  115. WARN_ON(i915_verify_lists(dev));
  116. return 0;
  117. }
  118. static inline bool
  119. i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
  120. {
  121. return obj->gtt_space && !obj->active;
  122. }
  123. int
  124. i915_gem_init_ioctl(struct drm_device *dev, void *data,
  125. struct drm_file *file)
  126. {
  127. struct drm_i915_private *dev_priv = dev->dev_private;
  128. struct drm_i915_gem_init *args = data;
  129. if (drm_core_check_feature(dev, DRIVER_MODESET))
  130. return -ENODEV;
  131. if (args->gtt_start >= args->gtt_end ||
  132. (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
  133. return -EINVAL;
  134. /* GEM with user mode setting was never supported on ilk and later. */
  135. if (INTEL_INFO(dev)->gen >= 5)
  136. return -ENODEV;
  137. mutex_lock(&dev->struct_mutex);
  138. i915_gem_setup_global_gtt(dev, args->gtt_start, args->gtt_end,
  139. args->gtt_end);
  140. dev_priv->gtt.mappable_end = args->gtt_end;
  141. mutex_unlock(&dev->struct_mutex);
  142. return 0;
  143. }
  144. int
  145. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  146. struct drm_file *file)
  147. {
  148. struct drm_i915_private *dev_priv = dev->dev_private;
  149. struct drm_i915_gem_get_aperture *args = data;
  150. struct drm_i915_gem_object *obj;
  151. size_t pinned;
  152. pinned = 0;
  153. mutex_lock(&dev->struct_mutex);
  154. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  155. if (obj->pin_count)
  156. pinned += obj->gtt_space->size;
  157. mutex_unlock(&dev->struct_mutex);
  158. args->aper_size = dev_priv->gtt.total;
  159. args->aper_available_size = args->aper_size - pinned;
  160. return 0;
  161. }
  162. void *i915_gem_object_alloc(struct drm_device *dev)
  163. {
  164. struct drm_i915_private *dev_priv = dev->dev_private;
  165. return kmem_cache_alloc(dev_priv->slab, GFP_KERNEL | __GFP_ZERO);
  166. }
  167. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  168. {
  169. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  170. kmem_cache_free(dev_priv->slab, obj);
  171. }
  172. static int
  173. i915_gem_create(struct drm_file *file,
  174. struct drm_device *dev,
  175. uint64_t size,
  176. uint32_t *handle_p)
  177. {
  178. struct drm_i915_gem_object *obj;
  179. int ret;
  180. u32 handle;
  181. size = roundup(size, PAGE_SIZE);
  182. if (size == 0)
  183. return -EINVAL;
  184. /* Allocate the new object */
  185. obj = i915_gem_alloc_object(dev, size);
  186. if (obj == NULL)
  187. return -ENOMEM;
  188. ret = drm_gem_handle_create(file, &obj->base, &handle);
  189. if (ret) {
  190. drm_gem_object_release(&obj->base);
  191. i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
  192. i915_gem_object_free(obj);
  193. return ret;
  194. }
  195. /* drop reference from allocate - handle holds it now */
  196. drm_gem_object_unreference(&obj->base);
  197. trace_i915_gem_object_create(obj);
  198. *handle_p = handle;
  199. return 0;
  200. }
  201. int
  202. i915_gem_dumb_create(struct drm_file *file,
  203. struct drm_device *dev,
  204. struct drm_mode_create_dumb *args)
  205. {
  206. /* have to work out size/pitch and return them */
  207. args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
  208. args->size = args->pitch * args->height;
  209. return i915_gem_create(file, dev,
  210. args->size, &args->handle);
  211. }
  212. int i915_gem_dumb_destroy(struct drm_file *file,
  213. struct drm_device *dev,
  214. uint32_t handle)
  215. {
  216. return drm_gem_handle_delete(file, handle);
  217. }
  218. /**
  219. * Creates a new mm object and returns a handle to it.
  220. */
  221. int
  222. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  223. struct drm_file *file)
  224. {
  225. struct drm_i915_gem_create *args = data;
  226. return i915_gem_create(file, dev,
  227. args->size, &args->handle);
  228. }
  229. static inline int
  230. __copy_to_user_swizzled(char __user *cpu_vaddr,
  231. const char *gpu_vaddr, int gpu_offset,
  232. int length)
  233. {
  234. int ret, cpu_offset = 0;
  235. while (length > 0) {
  236. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  237. int this_length = min(cacheline_end - gpu_offset, length);
  238. int swizzled_gpu_offset = gpu_offset ^ 64;
  239. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  240. gpu_vaddr + swizzled_gpu_offset,
  241. this_length);
  242. if (ret)
  243. return ret + length;
  244. cpu_offset += this_length;
  245. gpu_offset += this_length;
  246. length -= this_length;
  247. }
  248. return 0;
  249. }
  250. static inline int
  251. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  252. const char __user *cpu_vaddr,
  253. int length)
  254. {
  255. int ret, cpu_offset = 0;
  256. while (length > 0) {
  257. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  258. int this_length = min(cacheline_end - gpu_offset, length);
  259. int swizzled_gpu_offset = gpu_offset ^ 64;
  260. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  261. cpu_vaddr + cpu_offset,
  262. this_length);
  263. if (ret)
  264. return ret + length;
  265. cpu_offset += this_length;
  266. gpu_offset += this_length;
  267. length -= this_length;
  268. }
  269. return 0;
  270. }
  271. /* Per-page copy function for the shmem pread fastpath.
  272. * Flushes invalid cachelines before reading the target if
  273. * needs_clflush is set. */
  274. static int
  275. shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
  276. char __user *user_data,
  277. bool page_do_bit17_swizzling, bool needs_clflush)
  278. {
  279. char *vaddr;
  280. int ret;
  281. if (unlikely(page_do_bit17_swizzling))
  282. return -EINVAL;
  283. vaddr = kmap_atomic(page);
  284. if (needs_clflush)
  285. drm_clflush_virt_range(vaddr + shmem_page_offset,
  286. page_length);
  287. ret = __copy_to_user_inatomic(user_data,
  288. vaddr + shmem_page_offset,
  289. page_length);
  290. kunmap_atomic(vaddr);
  291. return ret ? -EFAULT : 0;
  292. }
  293. static void
  294. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  295. bool swizzled)
  296. {
  297. if (unlikely(swizzled)) {
  298. unsigned long start = (unsigned long) addr;
  299. unsigned long end = (unsigned long) addr + length;
  300. /* For swizzling simply ensure that we always flush both
  301. * channels. Lame, but simple and it works. Swizzled
  302. * pwrite/pread is far from a hotpath - current userspace
  303. * doesn't use it at all. */
  304. start = round_down(start, 128);
  305. end = round_up(end, 128);
  306. drm_clflush_virt_range((void *)start, end - start);
  307. } else {
  308. drm_clflush_virt_range(addr, length);
  309. }
  310. }
  311. /* Only difference to the fast-path function is that this can handle bit17
  312. * and uses non-atomic copy and kmap functions. */
  313. static int
  314. shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
  315. char __user *user_data,
  316. bool page_do_bit17_swizzling, bool needs_clflush)
  317. {
  318. char *vaddr;
  319. int ret;
  320. vaddr = kmap(page);
  321. if (needs_clflush)
  322. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  323. page_length,
  324. page_do_bit17_swizzling);
  325. if (page_do_bit17_swizzling)
  326. ret = __copy_to_user_swizzled(user_data,
  327. vaddr, shmem_page_offset,
  328. page_length);
  329. else
  330. ret = __copy_to_user(user_data,
  331. vaddr + shmem_page_offset,
  332. page_length);
  333. kunmap(page);
  334. return ret ? - EFAULT : 0;
  335. }
  336. static int
  337. i915_gem_shmem_pread(struct drm_device *dev,
  338. struct drm_i915_gem_object *obj,
  339. struct drm_i915_gem_pread *args,
  340. struct drm_file *file)
  341. {
  342. char __user *user_data;
  343. ssize_t remain;
  344. loff_t offset;
  345. int shmem_page_offset, page_length, ret = 0;
  346. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  347. int prefaulted = 0;
  348. int needs_clflush = 0;
  349. struct sg_page_iter sg_iter;
  350. user_data = to_user_ptr(args->data_ptr);
  351. remain = args->size;
  352. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  353. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
  354. /* If we're not in the cpu read domain, set ourself into the gtt
  355. * read domain and manually flush cachelines (if required). This
  356. * optimizes for the case when the gpu will dirty the data
  357. * anyway again before the next pread happens. */
  358. if (obj->cache_level == I915_CACHE_NONE)
  359. needs_clflush = 1;
  360. if (obj->gtt_space) {
  361. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  362. if (ret)
  363. return ret;
  364. }
  365. }
  366. ret = i915_gem_object_get_pages(obj);
  367. if (ret)
  368. return ret;
  369. i915_gem_object_pin_pages(obj);
  370. offset = args->offset;
  371. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  372. offset >> PAGE_SHIFT) {
  373. struct page *page = sg_page_iter_page(&sg_iter);
  374. if (remain <= 0)
  375. break;
  376. /* Operation in this page
  377. *
  378. * shmem_page_offset = offset within page in shmem file
  379. * page_length = bytes to copy for this page
  380. */
  381. shmem_page_offset = offset_in_page(offset);
  382. page_length = remain;
  383. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  384. page_length = PAGE_SIZE - shmem_page_offset;
  385. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  386. (page_to_phys(page) & (1 << 17)) != 0;
  387. ret = shmem_pread_fast(page, shmem_page_offset, page_length,
  388. user_data, page_do_bit17_swizzling,
  389. needs_clflush);
  390. if (ret == 0)
  391. goto next_page;
  392. mutex_unlock(&dev->struct_mutex);
  393. if (!prefaulted) {
  394. ret = fault_in_multipages_writeable(user_data, remain);
  395. /* Userspace is tricking us, but we've already clobbered
  396. * its pages with the prefault and promised to write the
  397. * data up to the first fault. Hence ignore any errors
  398. * and just continue. */
  399. (void)ret;
  400. prefaulted = 1;
  401. }
  402. ret = shmem_pread_slow(page, shmem_page_offset, page_length,
  403. user_data, page_do_bit17_swizzling,
  404. needs_clflush);
  405. mutex_lock(&dev->struct_mutex);
  406. next_page:
  407. mark_page_accessed(page);
  408. if (ret)
  409. goto out;
  410. remain -= page_length;
  411. user_data += page_length;
  412. offset += page_length;
  413. }
  414. out:
  415. i915_gem_object_unpin_pages(obj);
  416. return ret;
  417. }
  418. /**
  419. * Reads data from the object referenced by handle.
  420. *
  421. * On error, the contents of *data are undefined.
  422. */
  423. int
  424. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  425. struct drm_file *file)
  426. {
  427. struct drm_i915_gem_pread *args = data;
  428. struct drm_i915_gem_object *obj;
  429. int ret = 0;
  430. if (args->size == 0)
  431. return 0;
  432. if (!access_ok(VERIFY_WRITE,
  433. to_user_ptr(args->data_ptr),
  434. args->size))
  435. return -EFAULT;
  436. ret = i915_mutex_lock_interruptible(dev);
  437. if (ret)
  438. return ret;
  439. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  440. if (&obj->base == NULL) {
  441. ret = -ENOENT;
  442. goto unlock;
  443. }
  444. /* Bounds check source. */
  445. if (args->offset > obj->base.size ||
  446. args->size > obj->base.size - args->offset) {
  447. ret = -EINVAL;
  448. goto out;
  449. }
  450. /* prime objects have no backing filp to GEM pread/pwrite
  451. * pages from.
  452. */
  453. if (!obj->base.filp) {
  454. ret = -EINVAL;
  455. goto out;
  456. }
  457. trace_i915_gem_object_pread(obj, args->offset, args->size);
  458. ret = i915_gem_shmem_pread(dev, obj, args, file);
  459. out:
  460. drm_gem_object_unreference(&obj->base);
  461. unlock:
  462. mutex_unlock(&dev->struct_mutex);
  463. return ret;
  464. }
  465. /* This is the fast write path which cannot handle
  466. * page faults in the source data
  467. */
  468. static inline int
  469. fast_user_write(struct io_mapping *mapping,
  470. loff_t page_base, int page_offset,
  471. char __user *user_data,
  472. int length)
  473. {
  474. void __iomem *vaddr_atomic;
  475. void *vaddr;
  476. unsigned long unwritten;
  477. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  478. /* We can use the cpu mem copy function because this is X86. */
  479. vaddr = (void __force*)vaddr_atomic + page_offset;
  480. unwritten = __copy_from_user_inatomic_nocache(vaddr,
  481. user_data, length);
  482. io_mapping_unmap_atomic(vaddr_atomic);
  483. return unwritten;
  484. }
  485. /**
  486. * This is the fast pwrite path, where we copy the data directly from the
  487. * user into the GTT, uncached.
  488. */
  489. static int
  490. i915_gem_gtt_pwrite_fast(struct drm_device *dev,
  491. struct drm_i915_gem_object *obj,
  492. struct drm_i915_gem_pwrite *args,
  493. struct drm_file *file)
  494. {
  495. drm_i915_private_t *dev_priv = dev->dev_private;
  496. ssize_t remain;
  497. loff_t offset, page_base;
  498. char __user *user_data;
  499. int page_offset, page_length, ret;
  500. ret = i915_gem_object_pin(obj, 0, true, true);
  501. if (ret)
  502. goto out;
  503. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  504. if (ret)
  505. goto out_unpin;
  506. ret = i915_gem_object_put_fence(obj);
  507. if (ret)
  508. goto out_unpin;
  509. user_data = to_user_ptr(args->data_ptr);
  510. remain = args->size;
  511. offset = obj->gtt_offset + args->offset;
  512. while (remain > 0) {
  513. /* Operation in this page
  514. *
  515. * page_base = page offset within aperture
  516. * page_offset = offset within page
  517. * page_length = bytes to copy for this page
  518. */
  519. page_base = offset & PAGE_MASK;
  520. page_offset = offset_in_page(offset);
  521. page_length = remain;
  522. if ((page_offset + remain) > PAGE_SIZE)
  523. page_length = PAGE_SIZE - page_offset;
  524. /* If we get a fault while copying data, then (presumably) our
  525. * source page isn't available. Return the error and we'll
  526. * retry in the slow path.
  527. */
  528. if (fast_user_write(dev_priv->gtt.mappable, page_base,
  529. page_offset, user_data, page_length)) {
  530. ret = -EFAULT;
  531. goto out_unpin;
  532. }
  533. remain -= page_length;
  534. user_data += page_length;
  535. offset += page_length;
  536. }
  537. out_unpin:
  538. i915_gem_object_unpin(obj);
  539. out:
  540. return ret;
  541. }
  542. /* Per-page copy function for the shmem pwrite fastpath.
  543. * Flushes invalid cachelines before writing to the target if
  544. * needs_clflush_before is set and flushes out any written cachelines after
  545. * writing if needs_clflush is set. */
  546. static int
  547. shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
  548. char __user *user_data,
  549. bool page_do_bit17_swizzling,
  550. bool needs_clflush_before,
  551. bool needs_clflush_after)
  552. {
  553. char *vaddr;
  554. int ret;
  555. if (unlikely(page_do_bit17_swizzling))
  556. return -EINVAL;
  557. vaddr = kmap_atomic(page);
  558. if (needs_clflush_before)
  559. drm_clflush_virt_range(vaddr + shmem_page_offset,
  560. page_length);
  561. ret = __copy_from_user_inatomic_nocache(vaddr + shmem_page_offset,
  562. user_data,
  563. page_length);
  564. if (needs_clflush_after)
  565. drm_clflush_virt_range(vaddr + shmem_page_offset,
  566. page_length);
  567. kunmap_atomic(vaddr);
  568. return ret ? -EFAULT : 0;
  569. }
  570. /* Only difference to the fast-path function is that this can handle bit17
  571. * and uses non-atomic copy and kmap functions. */
  572. static int
  573. shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
  574. char __user *user_data,
  575. bool page_do_bit17_swizzling,
  576. bool needs_clflush_before,
  577. bool needs_clflush_after)
  578. {
  579. char *vaddr;
  580. int ret;
  581. vaddr = kmap(page);
  582. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  583. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  584. page_length,
  585. page_do_bit17_swizzling);
  586. if (page_do_bit17_swizzling)
  587. ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
  588. user_data,
  589. page_length);
  590. else
  591. ret = __copy_from_user(vaddr + shmem_page_offset,
  592. user_data,
  593. page_length);
  594. if (needs_clflush_after)
  595. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  596. page_length,
  597. page_do_bit17_swizzling);
  598. kunmap(page);
  599. return ret ? -EFAULT : 0;
  600. }
  601. static int
  602. i915_gem_shmem_pwrite(struct drm_device *dev,
  603. struct drm_i915_gem_object *obj,
  604. struct drm_i915_gem_pwrite *args,
  605. struct drm_file *file)
  606. {
  607. ssize_t remain;
  608. loff_t offset;
  609. char __user *user_data;
  610. int shmem_page_offset, page_length, ret = 0;
  611. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  612. int hit_slowpath = 0;
  613. int needs_clflush_after = 0;
  614. int needs_clflush_before = 0;
  615. struct sg_page_iter sg_iter;
  616. user_data = to_user_ptr(args->data_ptr);
  617. remain = args->size;
  618. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  619. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  620. /* If we're not in the cpu write domain, set ourself into the gtt
  621. * write domain and manually flush cachelines (if required). This
  622. * optimizes for the case when the gpu will use the data
  623. * right away and we therefore have to clflush anyway. */
  624. if (obj->cache_level == I915_CACHE_NONE)
  625. needs_clflush_after = 1;
  626. if (obj->gtt_space) {
  627. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  628. if (ret)
  629. return ret;
  630. }
  631. }
  632. /* Same trick applies for invalidate partially written cachelines before
  633. * writing. */
  634. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)
  635. && obj->cache_level == I915_CACHE_NONE)
  636. needs_clflush_before = 1;
  637. ret = i915_gem_object_get_pages(obj);
  638. if (ret)
  639. return ret;
  640. i915_gem_object_pin_pages(obj);
  641. offset = args->offset;
  642. obj->dirty = 1;
  643. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  644. offset >> PAGE_SHIFT) {
  645. struct page *page = sg_page_iter_page(&sg_iter);
  646. int partial_cacheline_write;
  647. if (remain <= 0)
  648. break;
  649. /* Operation in this page
  650. *
  651. * shmem_page_offset = offset within page in shmem file
  652. * page_length = bytes to copy for this page
  653. */
  654. shmem_page_offset = offset_in_page(offset);
  655. page_length = remain;
  656. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  657. page_length = PAGE_SIZE - shmem_page_offset;
  658. /* If we don't overwrite a cacheline completely we need to be
  659. * careful to have up-to-date data by first clflushing. Don't
  660. * overcomplicate things and flush the entire patch. */
  661. partial_cacheline_write = needs_clflush_before &&
  662. ((shmem_page_offset | page_length)
  663. & (boot_cpu_data.x86_clflush_size - 1));
  664. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  665. (page_to_phys(page) & (1 << 17)) != 0;
  666. ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
  667. user_data, page_do_bit17_swizzling,
  668. partial_cacheline_write,
  669. needs_clflush_after);
  670. if (ret == 0)
  671. goto next_page;
  672. hit_slowpath = 1;
  673. mutex_unlock(&dev->struct_mutex);
  674. ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
  675. user_data, page_do_bit17_swizzling,
  676. partial_cacheline_write,
  677. needs_clflush_after);
  678. mutex_lock(&dev->struct_mutex);
  679. next_page:
  680. set_page_dirty(page);
  681. mark_page_accessed(page);
  682. if (ret)
  683. goto out;
  684. remain -= page_length;
  685. user_data += page_length;
  686. offset += page_length;
  687. }
  688. out:
  689. i915_gem_object_unpin_pages(obj);
  690. if (hit_slowpath) {
  691. /*
  692. * Fixup: Flush cpu caches in case we didn't flush the dirty
  693. * cachelines in-line while writing and the object moved
  694. * out of the cpu write domain while we've dropped the lock.
  695. */
  696. if (!needs_clflush_after &&
  697. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  698. i915_gem_clflush_object(obj);
  699. i915_gem_chipset_flush(dev);
  700. }
  701. }
  702. if (needs_clflush_after)
  703. i915_gem_chipset_flush(dev);
  704. return ret;
  705. }
  706. /**
  707. * Writes data to the object referenced by handle.
  708. *
  709. * On error, the contents of the buffer that were to be modified are undefined.
  710. */
  711. int
  712. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  713. struct drm_file *file)
  714. {
  715. struct drm_i915_gem_pwrite *args = data;
  716. struct drm_i915_gem_object *obj;
  717. int ret;
  718. if (args->size == 0)
  719. return 0;
  720. if (!access_ok(VERIFY_READ,
  721. to_user_ptr(args->data_ptr),
  722. args->size))
  723. return -EFAULT;
  724. ret = fault_in_multipages_readable(to_user_ptr(args->data_ptr),
  725. args->size);
  726. if (ret)
  727. return -EFAULT;
  728. ret = i915_mutex_lock_interruptible(dev);
  729. if (ret)
  730. return ret;
  731. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  732. if (&obj->base == NULL) {
  733. ret = -ENOENT;
  734. goto unlock;
  735. }
  736. /* Bounds check destination. */
  737. if (args->offset > obj->base.size ||
  738. args->size > obj->base.size - args->offset) {
  739. ret = -EINVAL;
  740. goto out;
  741. }
  742. /* prime objects have no backing filp to GEM pread/pwrite
  743. * pages from.
  744. */
  745. if (!obj->base.filp) {
  746. ret = -EINVAL;
  747. goto out;
  748. }
  749. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  750. ret = -EFAULT;
  751. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  752. * it would end up going through the fenced access, and we'll get
  753. * different detiling behavior between reading and writing.
  754. * pread/pwrite currently are reading and writing from the CPU
  755. * perspective, requiring manual detiling by the client.
  756. */
  757. if (obj->phys_obj) {
  758. ret = i915_gem_phys_pwrite(dev, obj, args, file);
  759. goto out;
  760. }
  761. if (obj->cache_level == I915_CACHE_NONE &&
  762. obj->tiling_mode == I915_TILING_NONE &&
  763. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  764. ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
  765. /* Note that the gtt paths might fail with non-page-backed user
  766. * pointers (e.g. gtt mappings when moving data between
  767. * textures). Fallback to the shmem path in that case. */
  768. }
  769. if (ret == -EFAULT || ret == -ENOSPC)
  770. ret = i915_gem_shmem_pwrite(dev, obj, args, file);
  771. out:
  772. drm_gem_object_unreference(&obj->base);
  773. unlock:
  774. mutex_unlock(&dev->struct_mutex);
  775. return ret;
  776. }
  777. int
  778. i915_gem_check_wedge(struct i915_gpu_error *error,
  779. bool interruptible)
  780. {
  781. if (i915_reset_in_progress(error)) {
  782. /* Non-interruptible callers can't handle -EAGAIN, hence return
  783. * -EIO unconditionally for these. */
  784. if (!interruptible)
  785. return -EIO;
  786. /* Recovery complete, but the reset failed ... */
  787. if (i915_terminally_wedged(error))
  788. return -EIO;
  789. return -EAGAIN;
  790. }
  791. return 0;
  792. }
  793. /*
  794. * Compare seqno against outstanding lazy request. Emit a request if they are
  795. * equal.
  796. */
  797. static int
  798. i915_gem_check_olr(struct intel_ring_buffer *ring, u32 seqno)
  799. {
  800. int ret;
  801. BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex));
  802. ret = 0;
  803. if (seqno == ring->outstanding_lazy_request)
  804. ret = i915_add_request(ring, NULL);
  805. return ret;
  806. }
  807. /**
  808. * __wait_seqno - wait until execution of seqno has finished
  809. * @ring: the ring expected to report seqno
  810. * @seqno: duh!
  811. * @reset_counter: reset sequence associated with the given seqno
  812. * @interruptible: do an interruptible wait (normally yes)
  813. * @timeout: in - how long to wait (NULL forever); out - how much time remaining
  814. *
  815. * Note: It is of utmost importance that the passed in seqno and reset_counter
  816. * values have been read by the caller in an smp safe manner. Where read-side
  817. * locks are involved, it is sufficient to read the reset_counter before
  818. * unlocking the lock that protects the seqno. For lockless tricks, the
  819. * reset_counter _must_ be read before, and an appropriate smp_rmb must be
  820. * inserted.
  821. *
  822. * Returns 0 if the seqno was found within the alloted time. Else returns the
  823. * errno with remaining time filled in timeout argument.
  824. */
  825. static int __wait_seqno(struct intel_ring_buffer *ring, u32 seqno,
  826. unsigned reset_counter,
  827. bool interruptible, struct timespec *timeout)
  828. {
  829. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  830. struct timespec before, now, wait_time={1,0};
  831. unsigned long timeout_jiffies;
  832. long end;
  833. bool wait_forever = true;
  834. int ret;
  835. if (i915_seqno_passed(ring->get_seqno(ring, true), seqno))
  836. return 0;
  837. trace_i915_gem_request_wait_begin(ring, seqno);
  838. if (timeout != NULL) {
  839. wait_time = *timeout;
  840. wait_forever = false;
  841. }
  842. timeout_jiffies = timespec_to_jiffies_timeout(&wait_time);
  843. if (WARN_ON(!ring->irq_get(ring)))
  844. return -ENODEV;
  845. /* Record current time in case interrupted by signal, or wedged * */
  846. getrawmonotonic(&before);
  847. #define EXIT_COND \
  848. (i915_seqno_passed(ring->get_seqno(ring, false), seqno) || \
  849. i915_reset_in_progress(&dev_priv->gpu_error) || \
  850. reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  851. do {
  852. if (interruptible)
  853. end = wait_event_interruptible_timeout(ring->irq_queue,
  854. EXIT_COND,
  855. timeout_jiffies);
  856. else
  857. end = wait_event_timeout(ring->irq_queue, EXIT_COND,
  858. timeout_jiffies);
  859. /* We need to check whether any gpu reset happened in between
  860. * the caller grabbing the seqno and now ... */
  861. if (reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  862. end = -EAGAIN;
  863. /* ... but upgrade the -EGAIN to an -EIO if the gpu is truely
  864. * gone. */
  865. ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
  866. if (ret)
  867. end = ret;
  868. } while (end == 0 && wait_forever);
  869. getrawmonotonic(&now);
  870. ring->irq_put(ring);
  871. trace_i915_gem_request_wait_end(ring, seqno);
  872. #undef EXIT_COND
  873. if (timeout) {
  874. struct timespec sleep_time = timespec_sub(now, before);
  875. *timeout = timespec_sub(*timeout, sleep_time);
  876. if (!timespec_valid(timeout)) /* i.e. negative time remains */
  877. set_normalized_timespec(timeout, 0, 0);
  878. }
  879. switch (end) {
  880. case -EIO:
  881. case -EAGAIN: /* Wedged */
  882. case -ERESTARTSYS: /* Signal */
  883. return (int)end;
  884. case 0: /* Timeout */
  885. return -ETIME;
  886. default: /* Completed */
  887. WARN_ON(end < 0); /* We're not aware of other errors */
  888. return 0;
  889. }
  890. }
  891. /**
  892. * Waits for a sequence number to be signaled, and cleans up the
  893. * request and object lists appropriately for that event.
  894. */
  895. int
  896. i915_wait_seqno(struct intel_ring_buffer *ring, uint32_t seqno)
  897. {
  898. struct drm_device *dev = ring->dev;
  899. struct drm_i915_private *dev_priv = dev->dev_private;
  900. bool interruptible = dev_priv->mm.interruptible;
  901. int ret;
  902. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  903. BUG_ON(seqno == 0);
  904. ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
  905. if (ret)
  906. return ret;
  907. ret = i915_gem_check_olr(ring, seqno);
  908. if (ret)
  909. return ret;
  910. return __wait_seqno(ring, seqno,
  911. atomic_read(&dev_priv->gpu_error.reset_counter),
  912. interruptible, NULL);
  913. }
  914. static int
  915. i915_gem_object_wait_rendering__tail(struct drm_i915_gem_object *obj,
  916. struct intel_ring_buffer *ring)
  917. {
  918. i915_gem_retire_requests_ring(ring);
  919. /* Manually manage the write flush as we may have not yet
  920. * retired the buffer.
  921. *
  922. * Note that the last_write_seqno is always the earlier of
  923. * the two (read/write) seqno, so if we haved successfully waited,
  924. * we know we have passed the last write.
  925. */
  926. obj->last_write_seqno = 0;
  927. obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
  928. return 0;
  929. }
  930. /**
  931. * Ensures that all rendering to the object has completed and the object is
  932. * safe to unbind from the GTT or access from the CPU.
  933. */
  934. static __must_check int
  935. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  936. bool readonly)
  937. {
  938. struct intel_ring_buffer *ring = obj->ring;
  939. u32 seqno;
  940. int ret;
  941. seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
  942. if (seqno == 0)
  943. return 0;
  944. ret = i915_wait_seqno(ring, seqno);
  945. if (ret)
  946. return ret;
  947. return i915_gem_object_wait_rendering__tail(obj, ring);
  948. }
  949. /* A nonblocking variant of the above wait. This is a highly dangerous routine
  950. * as the object state may change during this call.
  951. */
  952. static __must_check int
  953. i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
  954. bool readonly)
  955. {
  956. struct drm_device *dev = obj->base.dev;
  957. struct drm_i915_private *dev_priv = dev->dev_private;
  958. struct intel_ring_buffer *ring = obj->ring;
  959. unsigned reset_counter;
  960. u32 seqno;
  961. int ret;
  962. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  963. BUG_ON(!dev_priv->mm.interruptible);
  964. seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
  965. if (seqno == 0)
  966. return 0;
  967. ret = i915_gem_check_wedge(&dev_priv->gpu_error, true);
  968. if (ret)
  969. return ret;
  970. ret = i915_gem_check_olr(ring, seqno);
  971. if (ret)
  972. return ret;
  973. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  974. mutex_unlock(&dev->struct_mutex);
  975. ret = __wait_seqno(ring, seqno, reset_counter, true, NULL);
  976. mutex_lock(&dev->struct_mutex);
  977. if (ret)
  978. return ret;
  979. return i915_gem_object_wait_rendering__tail(obj, ring);
  980. }
  981. /**
  982. * Called when user space prepares to use an object with the CPU, either
  983. * through the mmap ioctl's mapping or a GTT mapping.
  984. */
  985. int
  986. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  987. struct drm_file *file)
  988. {
  989. struct drm_i915_gem_set_domain *args = data;
  990. struct drm_i915_gem_object *obj;
  991. uint32_t read_domains = args->read_domains;
  992. uint32_t write_domain = args->write_domain;
  993. int ret;
  994. /* Only handle setting domains to types used by the CPU. */
  995. if (write_domain & I915_GEM_GPU_DOMAINS)
  996. return -EINVAL;
  997. if (read_domains & I915_GEM_GPU_DOMAINS)
  998. return -EINVAL;
  999. /* Having something in the write domain implies it's in the read
  1000. * domain, and only that read domain. Enforce that in the request.
  1001. */
  1002. if (write_domain != 0 && read_domains != write_domain)
  1003. return -EINVAL;
  1004. ret = i915_mutex_lock_interruptible(dev);
  1005. if (ret)
  1006. return ret;
  1007. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1008. if (&obj->base == NULL) {
  1009. ret = -ENOENT;
  1010. goto unlock;
  1011. }
  1012. /* Try to flush the object off the GPU without holding the lock.
  1013. * We will repeat the flush holding the lock in the normal manner
  1014. * to catch cases where we are gazumped.
  1015. */
  1016. ret = i915_gem_object_wait_rendering__nonblocking(obj, !write_domain);
  1017. if (ret)
  1018. goto unref;
  1019. if (read_domains & I915_GEM_DOMAIN_GTT) {
  1020. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  1021. /* Silently promote "you're not bound, there was nothing to do"
  1022. * to success, since the client was just asking us to
  1023. * make sure everything was done.
  1024. */
  1025. if (ret == -EINVAL)
  1026. ret = 0;
  1027. } else {
  1028. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  1029. }
  1030. unref:
  1031. drm_gem_object_unreference(&obj->base);
  1032. unlock:
  1033. mutex_unlock(&dev->struct_mutex);
  1034. return ret;
  1035. }
  1036. /**
  1037. * Called when user space has done writes to this buffer
  1038. */
  1039. int
  1040. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1041. struct drm_file *file)
  1042. {
  1043. struct drm_i915_gem_sw_finish *args = data;
  1044. struct drm_i915_gem_object *obj;
  1045. int ret = 0;
  1046. ret = i915_mutex_lock_interruptible(dev);
  1047. if (ret)
  1048. return ret;
  1049. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1050. if (&obj->base == NULL) {
  1051. ret = -ENOENT;
  1052. goto unlock;
  1053. }
  1054. /* Pinned buffers may be scanout, so flush the cache */
  1055. if (obj->pin_count)
  1056. i915_gem_object_flush_cpu_write_domain(obj);
  1057. drm_gem_object_unreference(&obj->base);
  1058. unlock:
  1059. mutex_unlock(&dev->struct_mutex);
  1060. return ret;
  1061. }
  1062. /**
  1063. * Maps the contents of an object, returning the address it is mapped
  1064. * into.
  1065. *
  1066. * While the mapping holds a reference on the contents of the object, it doesn't
  1067. * imply a ref on the object itself.
  1068. */
  1069. int
  1070. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1071. struct drm_file *file)
  1072. {
  1073. struct drm_i915_gem_mmap *args = data;
  1074. struct drm_gem_object *obj;
  1075. unsigned long addr;
  1076. obj = drm_gem_object_lookup(dev, file, args->handle);
  1077. if (obj == NULL)
  1078. return -ENOENT;
  1079. /* prime objects have no backing filp to GEM mmap
  1080. * pages from.
  1081. */
  1082. if (!obj->filp) {
  1083. drm_gem_object_unreference_unlocked(obj);
  1084. return -EINVAL;
  1085. }
  1086. addr = vm_mmap(obj->filp, 0, args->size,
  1087. PROT_READ | PROT_WRITE, MAP_SHARED,
  1088. args->offset);
  1089. drm_gem_object_unreference_unlocked(obj);
  1090. if (IS_ERR((void *)addr))
  1091. return addr;
  1092. args->addr_ptr = (uint64_t) addr;
  1093. return 0;
  1094. }
  1095. /**
  1096. * i915_gem_fault - fault a page into the GTT
  1097. * vma: VMA in question
  1098. * vmf: fault info
  1099. *
  1100. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1101. * from userspace. The fault handler takes care of binding the object to
  1102. * the GTT (if needed), allocating and programming a fence register (again,
  1103. * only if needed based on whether the old reg is still valid or the object
  1104. * is tiled) and inserting a new PTE into the faulting process.
  1105. *
  1106. * Note that the faulting process may involve evicting existing objects
  1107. * from the GTT and/or fence registers to make room. So performance may
  1108. * suffer if the GTT working set is large or there are few fence registers
  1109. * left.
  1110. */
  1111. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1112. {
  1113. struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
  1114. struct drm_device *dev = obj->base.dev;
  1115. drm_i915_private_t *dev_priv = dev->dev_private;
  1116. pgoff_t page_offset;
  1117. unsigned long pfn;
  1118. int ret = 0;
  1119. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1120. /* We don't use vmf->pgoff since that has the fake offset */
  1121. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  1122. PAGE_SHIFT;
  1123. ret = i915_mutex_lock_interruptible(dev);
  1124. if (ret)
  1125. goto out;
  1126. trace_i915_gem_object_fault(obj, page_offset, true, write);
  1127. /* Access to snoopable pages through the GTT is incoherent. */
  1128. if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
  1129. ret = -EINVAL;
  1130. goto unlock;
  1131. }
  1132. /* Now bind it into the GTT if needed */
  1133. ret = i915_gem_object_pin(obj, 0, true, false);
  1134. if (ret)
  1135. goto unlock;
  1136. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1137. if (ret)
  1138. goto unpin;
  1139. ret = i915_gem_object_get_fence(obj);
  1140. if (ret)
  1141. goto unpin;
  1142. obj->fault_mappable = true;
  1143. pfn = ((dev_priv->gtt.mappable_base + obj->gtt_offset) >> PAGE_SHIFT) +
  1144. page_offset;
  1145. /* Finally, remap it using the new GTT offset */
  1146. ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
  1147. unpin:
  1148. i915_gem_object_unpin(obj);
  1149. unlock:
  1150. mutex_unlock(&dev->struct_mutex);
  1151. out:
  1152. switch (ret) {
  1153. case -EIO:
  1154. /* If this -EIO is due to a gpu hang, give the reset code a
  1155. * chance to clean up the mess. Otherwise return the proper
  1156. * SIGBUS. */
  1157. if (i915_terminally_wedged(&dev_priv->gpu_error))
  1158. return VM_FAULT_SIGBUS;
  1159. case -EAGAIN:
  1160. /* Give the error handler a chance to run and move the
  1161. * objects off the GPU active list. Next time we service the
  1162. * fault, we should be able to transition the page into the
  1163. * GTT without touching the GPU (and so avoid further
  1164. * EIO/EGAIN). If the GPU is wedged, then there is no issue
  1165. * with coherency, just lost writes.
  1166. */
  1167. set_need_resched();
  1168. case 0:
  1169. case -ERESTARTSYS:
  1170. case -EINTR:
  1171. case -EBUSY:
  1172. /*
  1173. * EBUSY is ok: this just means that another thread
  1174. * already did the job.
  1175. */
  1176. return VM_FAULT_NOPAGE;
  1177. case -ENOMEM:
  1178. return VM_FAULT_OOM;
  1179. case -ENOSPC:
  1180. return VM_FAULT_SIGBUS;
  1181. default:
  1182. WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
  1183. return VM_FAULT_SIGBUS;
  1184. }
  1185. }
  1186. /**
  1187. * i915_gem_release_mmap - remove physical page mappings
  1188. * @obj: obj in question
  1189. *
  1190. * Preserve the reservation of the mmapping with the DRM core code, but
  1191. * relinquish ownership of the pages back to the system.
  1192. *
  1193. * It is vital that we remove the page mapping if we have mapped a tiled
  1194. * object through the GTT and then lose the fence register due to
  1195. * resource pressure. Similarly if the object has been moved out of the
  1196. * aperture, than pages mapped into userspace must be revoked. Removing the
  1197. * mapping will then trigger a page fault on the next user access, allowing
  1198. * fixup by i915_gem_fault().
  1199. */
  1200. void
  1201. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1202. {
  1203. if (!obj->fault_mappable)
  1204. return;
  1205. if (obj->base.dev->dev_mapping)
  1206. unmap_mapping_range(obj->base.dev->dev_mapping,
  1207. (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
  1208. obj->base.size, 1);
  1209. obj->fault_mappable = false;
  1210. }
  1211. uint32_t
  1212. i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
  1213. {
  1214. uint32_t gtt_size;
  1215. if (INTEL_INFO(dev)->gen >= 4 ||
  1216. tiling_mode == I915_TILING_NONE)
  1217. return size;
  1218. /* Previous chips need a power-of-two fence region when tiling */
  1219. if (INTEL_INFO(dev)->gen == 3)
  1220. gtt_size = 1024*1024;
  1221. else
  1222. gtt_size = 512*1024;
  1223. while (gtt_size < size)
  1224. gtt_size <<= 1;
  1225. return gtt_size;
  1226. }
  1227. /**
  1228. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1229. * @obj: object to check
  1230. *
  1231. * Return the required GTT alignment for an object, taking into account
  1232. * potential fence register mapping.
  1233. */
  1234. uint32_t
  1235. i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
  1236. int tiling_mode, bool fenced)
  1237. {
  1238. /*
  1239. * Minimum alignment is 4k (GTT page size), but might be greater
  1240. * if a fence register is needed for the object.
  1241. */
  1242. if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
  1243. tiling_mode == I915_TILING_NONE)
  1244. return 4096;
  1245. /*
  1246. * Previous chips need to be aligned to the size of the smallest
  1247. * fence register that can contain the object.
  1248. */
  1249. return i915_gem_get_gtt_size(dev, size, tiling_mode);
  1250. }
  1251. static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  1252. {
  1253. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1254. int ret;
  1255. if (obj->base.map_list.map)
  1256. return 0;
  1257. dev_priv->mm.shrinker_no_lock_stealing = true;
  1258. ret = drm_gem_create_mmap_offset(&obj->base);
  1259. if (ret != -ENOSPC)
  1260. goto out;
  1261. /* Badly fragmented mmap space? The only way we can recover
  1262. * space is by destroying unwanted objects. We can't randomly release
  1263. * mmap_offsets as userspace expects them to be persistent for the
  1264. * lifetime of the objects. The closest we can is to release the
  1265. * offsets on purgeable objects by truncating it and marking it purged,
  1266. * which prevents userspace from ever using that object again.
  1267. */
  1268. i915_gem_purge(dev_priv, obj->base.size >> PAGE_SHIFT);
  1269. ret = drm_gem_create_mmap_offset(&obj->base);
  1270. if (ret != -ENOSPC)
  1271. goto out;
  1272. i915_gem_shrink_all(dev_priv);
  1273. ret = drm_gem_create_mmap_offset(&obj->base);
  1274. out:
  1275. dev_priv->mm.shrinker_no_lock_stealing = false;
  1276. return ret;
  1277. }
  1278. static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
  1279. {
  1280. if (!obj->base.map_list.map)
  1281. return;
  1282. drm_gem_free_mmap_offset(&obj->base);
  1283. }
  1284. int
  1285. i915_gem_mmap_gtt(struct drm_file *file,
  1286. struct drm_device *dev,
  1287. uint32_t handle,
  1288. uint64_t *offset)
  1289. {
  1290. struct drm_i915_private *dev_priv = dev->dev_private;
  1291. struct drm_i915_gem_object *obj;
  1292. int ret;
  1293. ret = i915_mutex_lock_interruptible(dev);
  1294. if (ret)
  1295. return ret;
  1296. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  1297. if (&obj->base == NULL) {
  1298. ret = -ENOENT;
  1299. goto unlock;
  1300. }
  1301. if (obj->base.size > dev_priv->gtt.mappable_end) {
  1302. ret = -E2BIG;
  1303. goto out;
  1304. }
  1305. if (obj->madv != I915_MADV_WILLNEED) {
  1306. DRM_ERROR("Attempting to mmap a purgeable buffer\n");
  1307. ret = -EINVAL;
  1308. goto out;
  1309. }
  1310. ret = i915_gem_object_create_mmap_offset(obj);
  1311. if (ret)
  1312. goto out;
  1313. *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
  1314. out:
  1315. drm_gem_object_unreference(&obj->base);
  1316. unlock:
  1317. mutex_unlock(&dev->struct_mutex);
  1318. return ret;
  1319. }
  1320. /**
  1321. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1322. * @dev: DRM device
  1323. * @data: GTT mapping ioctl data
  1324. * @file: GEM object info
  1325. *
  1326. * Simply returns the fake offset to userspace so it can mmap it.
  1327. * The mmap call will end up in drm_gem_mmap(), which will set things
  1328. * up so we can get faults in the handler above.
  1329. *
  1330. * The fault handler will take care of binding the object into the GTT
  1331. * (since it may have been evicted to make room for something), allocating
  1332. * a fence register, and mapping the appropriate aperture address into
  1333. * userspace.
  1334. */
  1335. int
  1336. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1337. struct drm_file *file)
  1338. {
  1339. struct drm_i915_gem_mmap_gtt *args = data;
  1340. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1341. }
  1342. /* Immediately discard the backing storage */
  1343. static void
  1344. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1345. {
  1346. struct inode *inode;
  1347. i915_gem_object_free_mmap_offset(obj);
  1348. if (obj->base.filp == NULL)
  1349. return;
  1350. /* Our goal here is to return as much of the memory as
  1351. * is possible back to the system as we are called from OOM.
  1352. * To do this we must instruct the shmfs to drop all of its
  1353. * backing pages, *now*.
  1354. */
  1355. inode = file_inode(obj->base.filp);
  1356. shmem_truncate_range(inode, 0, (loff_t)-1);
  1357. obj->madv = __I915_MADV_PURGED;
  1358. }
  1359. static inline int
  1360. i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
  1361. {
  1362. return obj->madv == I915_MADV_DONTNEED;
  1363. }
  1364. static void
  1365. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
  1366. {
  1367. struct sg_page_iter sg_iter;
  1368. int ret;
  1369. BUG_ON(obj->madv == __I915_MADV_PURGED);
  1370. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  1371. if (ret) {
  1372. /* In the event of a disaster, abandon all caches and
  1373. * hope for the best.
  1374. */
  1375. WARN_ON(ret != -EIO);
  1376. i915_gem_clflush_object(obj);
  1377. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  1378. }
  1379. if (i915_gem_object_needs_bit17_swizzle(obj))
  1380. i915_gem_object_save_bit_17_swizzle(obj);
  1381. if (obj->madv == I915_MADV_DONTNEED)
  1382. obj->dirty = 0;
  1383. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  1384. struct page *page = sg_page_iter_page(&sg_iter);
  1385. if (obj->dirty)
  1386. set_page_dirty(page);
  1387. if (obj->madv == I915_MADV_WILLNEED)
  1388. mark_page_accessed(page);
  1389. page_cache_release(page);
  1390. }
  1391. obj->dirty = 0;
  1392. sg_free_table(obj->pages);
  1393. kfree(obj->pages);
  1394. }
  1395. int
  1396. i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
  1397. {
  1398. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1399. if (obj->pages == NULL)
  1400. return 0;
  1401. BUG_ON(obj->gtt_space);
  1402. if (obj->pages_pin_count)
  1403. return -EBUSY;
  1404. /* ->put_pages might need to allocate memory for the bit17 swizzle
  1405. * array, hence protect them from being reaped by removing them from gtt
  1406. * lists early. */
  1407. list_del(&obj->global_list);
  1408. ops->put_pages(obj);
  1409. obj->pages = NULL;
  1410. if (i915_gem_object_is_purgeable(obj))
  1411. i915_gem_object_truncate(obj);
  1412. return 0;
  1413. }
  1414. static long
  1415. __i915_gem_shrink(struct drm_i915_private *dev_priv, long target,
  1416. bool purgeable_only)
  1417. {
  1418. struct drm_i915_gem_object *obj, *next;
  1419. long count = 0;
  1420. list_for_each_entry_safe(obj, next,
  1421. &dev_priv->mm.unbound_list,
  1422. global_list) {
  1423. if ((i915_gem_object_is_purgeable(obj) || !purgeable_only) &&
  1424. i915_gem_object_put_pages(obj) == 0) {
  1425. count += obj->base.size >> PAGE_SHIFT;
  1426. if (count >= target)
  1427. return count;
  1428. }
  1429. }
  1430. list_for_each_entry_safe(obj, next,
  1431. &dev_priv->mm.inactive_list,
  1432. mm_list) {
  1433. if ((i915_gem_object_is_purgeable(obj) || !purgeable_only) &&
  1434. i915_gem_object_unbind(obj) == 0 &&
  1435. i915_gem_object_put_pages(obj) == 0) {
  1436. count += obj->base.size >> PAGE_SHIFT;
  1437. if (count >= target)
  1438. return count;
  1439. }
  1440. }
  1441. return count;
  1442. }
  1443. static long
  1444. i915_gem_purge(struct drm_i915_private *dev_priv, long target)
  1445. {
  1446. return __i915_gem_shrink(dev_priv, target, true);
  1447. }
  1448. static void
  1449. i915_gem_shrink_all(struct drm_i915_private *dev_priv)
  1450. {
  1451. struct drm_i915_gem_object *obj, *next;
  1452. i915_gem_evict_everything(dev_priv->dev);
  1453. list_for_each_entry_safe(obj, next, &dev_priv->mm.unbound_list,
  1454. global_list)
  1455. i915_gem_object_put_pages(obj);
  1456. }
  1457. static int
  1458. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  1459. {
  1460. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1461. int page_count, i;
  1462. struct address_space *mapping;
  1463. struct sg_table *st;
  1464. struct scatterlist *sg;
  1465. struct sg_page_iter sg_iter;
  1466. struct page *page;
  1467. unsigned long last_pfn = 0; /* suppress gcc warning */
  1468. gfp_t gfp;
  1469. /* Assert that the object is not currently in any GPU domain. As it
  1470. * wasn't in the GTT, there shouldn't be any way it could have been in
  1471. * a GPU cache
  1472. */
  1473. BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  1474. BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  1475. st = kmalloc(sizeof(*st), GFP_KERNEL);
  1476. if (st == NULL)
  1477. return -ENOMEM;
  1478. page_count = obj->base.size / PAGE_SIZE;
  1479. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  1480. sg_free_table(st);
  1481. kfree(st);
  1482. return -ENOMEM;
  1483. }
  1484. /* Get the list of pages out of our struct file. They'll be pinned
  1485. * at this point until we release them.
  1486. *
  1487. * Fail silently without starting the shrinker
  1488. */
  1489. mapping = file_inode(obj->base.filp)->i_mapping;
  1490. gfp = mapping_gfp_mask(mapping);
  1491. gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
  1492. gfp &= ~(__GFP_IO | __GFP_WAIT);
  1493. sg = st->sgl;
  1494. st->nents = 0;
  1495. for (i = 0; i < page_count; i++) {
  1496. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1497. if (IS_ERR(page)) {
  1498. i915_gem_purge(dev_priv, page_count);
  1499. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1500. }
  1501. if (IS_ERR(page)) {
  1502. /* We've tried hard to allocate the memory by reaping
  1503. * our own buffer, now let the real VM do its job and
  1504. * go down in flames if truly OOM.
  1505. */
  1506. gfp &= ~(__GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD);
  1507. gfp |= __GFP_IO | __GFP_WAIT;
  1508. i915_gem_shrink_all(dev_priv);
  1509. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1510. if (IS_ERR(page))
  1511. goto err_pages;
  1512. gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
  1513. gfp &= ~(__GFP_IO | __GFP_WAIT);
  1514. }
  1515. #ifdef CONFIG_SWIOTLB
  1516. if (swiotlb_nr_tbl()) {
  1517. st->nents++;
  1518. sg_set_page(sg, page, PAGE_SIZE, 0);
  1519. sg = sg_next(sg);
  1520. continue;
  1521. }
  1522. #endif
  1523. if (!i || page_to_pfn(page) != last_pfn + 1) {
  1524. if (i)
  1525. sg = sg_next(sg);
  1526. st->nents++;
  1527. sg_set_page(sg, page, PAGE_SIZE, 0);
  1528. } else {
  1529. sg->length += PAGE_SIZE;
  1530. }
  1531. last_pfn = page_to_pfn(page);
  1532. }
  1533. #ifdef CONFIG_SWIOTLB
  1534. if (!swiotlb_nr_tbl())
  1535. #endif
  1536. sg_mark_end(sg);
  1537. obj->pages = st;
  1538. if (i915_gem_object_needs_bit17_swizzle(obj))
  1539. i915_gem_object_do_bit_17_swizzle(obj);
  1540. return 0;
  1541. err_pages:
  1542. sg_mark_end(sg);
  1543. for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
  1544. page_cache_release(sg_page_iter_page(&sg_iter));
  1545. sg_free_table(st);
  1546. kfree(st);
  1547. return PTR_ERR(page);
  1548. }
  1549. /* Ensure that the associated pages are gathered from the backing storage
  1550. * and pinned into our object. i915_gem_object_get_pages() may be called
  1551. * multiple times before they are released by a single call to
  1552. * i915_gem_object_put_pages() - once the pages are no longer referenced
  1553. * either as a result of memory pressure (reaping pages under the shrinker)
  1554. * or as the object is itself released.
  1555. */
  1556. int
  1557. i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  1558. {
  1559. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1560. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1561. int ret;
  1562. if (obj->pages)
  1563. return 0;
  1564. if (obj->madv != I915_MADV_WILLNEED) {
  1565. DRM_ERROR("Attempting to obtain a purgeable object\n");
  1566. return -EINVAL;
  1567. }
  1568. BUG_ON(obj->pages_pin_count);
  1569. ret = ops->get_pages(obj);
  1570. if (ret)
  1571. return ret;
  1572. list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  1573. return 0;
  1574. }
  1575. void
  1576. i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
  1577. struct intel_ring_buffer *ring)
  1578. {
  1579. struct drm_device *dev = obj->base.dev;
  1580. struct drm_i915_private *dev_priv = dev->dev_private;
  1581. u32 seqno = intel_ring_get_seqno(ring);
  1582. BUG_ON(ring == NULL);
  1583. obj->ring = ring;
  1584. /* Add a reference if we're newly entering the active list. */
  1585. if (!obj->active) {
  1586. drm_gem_object_reference(&obj->base);
  1587. obj->active = 1;
  1588. }
  1589. /* Move from whatever list we were on to the tail of execution. */
  1590. list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
  1591. list_move_tail(&obj->ring_list, &ring->active_list);
  1592. obj->last_read_seqno = seqno;
  1593. if (obj->fenced_gpu_access) {
  1594. obj->last_fenced_seqno = seqno;
  1595. /* Bump MRU to take account of the delayed flush */
  1596. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  1597. struct drm_i915_fence_reg *reg;
  1598. reg = &dev_priv->fence_regs[obj->fence_reg];
  1599. list_move_tail(&reg->lru_list,
  1600. &dev_priv->mm.fence_list);
  1601. }
  1602. }
  1603. }
  1604. static void
  1605. i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
  1606. {
  1607. struct drm_device *dev = obj->base.dev;
  1608. struct drm_i915_private *dev_priv = dev->dev_private;
  1609. BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS);
  1610. BUG_ON(!obj->active);
  1611. list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  1612. list_del_init(&obj->ring_list);
  1613. obj->ring = NULL;
  1614. obj->last_read_seqno = 0;
  1615. obj->last_write_seqno = 0;
  1616. obj->base.write_domain = 0;
  1617. obj->last_fenced_seqno = 0;
  1618. obj->fenced_gpu_access = false;
  1619. obj->active = 0;
  1620. drm_gem_object_unreference(&obj->base);
  1621. WARN_ON(i915_verify_lists(dev));
  1622. }
  1623. static int
  1624. i915_gem_init_seqno(struct drm_device *dev, u32 seqno)
  1625. {
  1626. struct drm_i915_private *dev_priv = dev->dev_private;
  1627. struct intel_ring_buffer *ring;
  1628. int ret, i, j;
  1629. /* Carefully retire all requests without writing to the rings */
  1630. for_each_ring(ring, dev_priv, i) {
  1631. ret = intel_ring_idle(ring);
  1632. if (ret)
  1633. return ret;
  1634. }
  1635. i915_gem_retire_requests(dev);
  1636. /* Finally reset hw state */
  1637. for_each_ring(ring, dev_priv, i) {
  1638. intel_ring_init_seqno(ring, seqno);
  1639. for (j = 0; j < ARRAY_SIZE(ring->sync_seqno); j++)
  1640. ring->sync_seqno[j] = 0;
  1641. }
  1642. return 0;
  1643. }
  1644. int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
  1645. {
  1646. struct drm_i915_private *dev_priv = dev->dev_private;
  1647. int ret;
  1648. if (seqno == 0)
  1649. return -EINVAL;
  1650. /* HWS page needs to be set less than what we
  1651. * will inject to ring
  1652. */
  1653. ret = i915_gem_init_seqno(dev, seqno - 1);
  1654. if (ret)
  1655. return ret;
  1656. /* Carefully set the last_seqno value so that wrap
  1657. * detection still works
  1658. */
  1659. dev_priv->next_seqno = seqno;
  1660. dev_priv->last_seqno = seqno - 1;
  1661. if (dev_priv->last_seqno == 0)
  1662. dev_priv->last_seqno--;
  1663. return 0;
  1664. }
  1665. int
  1666. i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
  1667. {
  1668. struct drm_i915_private *dev_priv = dev->dev_private;
  1669. /* reserve 0 for non-seqno */
  1670. if (dev_priv->next_seqno == 0) {
  1671. int ret = i915_gem_init_seqno(dev, 0);
  1672. if (ret)
  1673. return ret;
  1674. dev_priv->next_seqno = 1;
  1675. }
  1676. *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
  1677. return 0;
  1678. }
  1679. int __i915_add_request(struct intel_ring_buffer *ring,
  1680. struct drm_file *file,
  1681. struct drm_i915_gem_object *obj,
  1682. u32 *out_seqno)
  1683. {
  1684. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  1685. struct drm_i915_gem_request *request;
  1686. u32 request_ring_position, request_start;
  1687. int was_empty;
  1688. int ret;
  1689. request_start = intel_ring_get_tail(ring);
  1690. /*
  1691. * Emit any outstanding flushes - execbuf can fail to emit the flush
  1692. * after having emitted the batchbuffer command. Hence we need to fix
  1693. * things up similar to emitting the lazy request. The difference here
  1694. * is that the flush _must_ happen before the next request, no matter
  1695. * what.
  1696. */
  1697. ret = intel_ring_flush_all_caches(ring);
  1698. if (ret)
  1699. return ret;
  1700. request = kmalloc(sizeof(*request), GFP_KERNEL);
  1701. if (request == NULL)
  1702. return -ENOMEM;
  1703. /* Record the position of the start of the request so that
  1704. * should we detect the updated seqno part-way through the
  1705. * GPU processing the request, we never over-estimate the
  1706. * position of the head.
  1707. */
  1708. request_ring_position = intel_ring_get_tail(ring);
  1709. ret = ring->add_request(ring);
  1710. if (ret) {
  1711. kfree(request);
  1712. return ret;
  1713. }
  1714. request->seqno = intel_ring_get_seqno(ring);
  1715. request->ring = ring;
  1716. request->head = request_start;
  1717. request->tail = request_ring_position;
  1718. request->ctx = ring->last_context;
  1719. request->batch_obj = obj;
  1720. /* Whilst this request exists, batch_obj will be on the
  1721. * active_list, and so will hold the active reference. Only when this
  1722. * request is retired will the the batch_obj be moved onto the
  1723. * inactive_list and lose its active reference. Hence we do not need
  1724. * to explicitly hold another reference here.
  1725. */
  1726. if (request->ctx)
  1727. i915_gem_context_reference(request->ctx);
  1728. request->emitted_jiffies = jiffies;
  1729. was_empty = list_empty(&ring->request_list);
  1730. list_add_tail(&request->list, &ring->request_list);
  1731. request->file_priv = NULL;
  1732. if (file) {
  1733. struct drm_i915_file_private *file_priv = file->driver_priv;
  1734. spin_lock(&file_priv->mm.lock);
  1735. request->file_priv = file_priv;
  1736. list_add_tail(&request->client_list,
  1737. &file_priv->mm.request_list);
  1738. spin_unlock(&file_priv->mm.lock);
  1739. }
  1740. trace_i915_gem_request_add(ring, request->seqno);
  1741. ring->outstanding_lazy_request = 0;
  1742. if (!dev_priv->mm.suspended) {
  1743. if (i915_enable_hangcheck) {
  1744. mod_timer(&dev_priv->gpu_error.hangcheck_timer,
  1745. round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
  1746. }
  1747. if (was_empty) {
  1748. queue_delayed_work(dev_priv->wq,
  1749. &dev_priv->mm.retire_work,
  1750. round_jiffies_up_relative(HZ));
  1751. intel_mark_busy(dev_priv->dev);
  1752. }
  1753. }
  1754. if (out_seqno)
  1755. *out_seqno = request->seqno;
  1756. return 0;
  1757. }
  1758. static inline void
  1759. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  1760. {
  1761. struct drm_i915_file_private *file_priv = request->file_priv;
  1762. if (!file_priv)
  1763. return;
  1764. spin_lock(&file_priv->mm.lock);
  1765. if (request->file_priv) {
  1766. list_del(&request->client_list);
  1767. request->file_priv = NULL;
  1768. }
  1769. spin_unlock(&file_priv->mm.lock);
  1770. }
  1771. static bool i915_head_inside_object(u32 acthd, struct drm_i915_gem_object *obj)
  1772. {
  1773. if (acthd >= obj->gtt_offset &&
  1774. acthd < obj->gtt_offset + obj->base.size)
  1775. return true;
  1776. return false;
  1777. }
  1778. static bool i915_head_inside_request(const u32 acthd_unmasked,
  1779. const u32 request_start,
  1780. const u32 request_end)
  1781. {
  1782. const u32 acthd = acthd_unmasked & HEAD_ADDR;
  1783. if (request_start < request_end) {
  1784. if (acthd >= request_start && acthd < request_end)
  1785. return true;
  1786. } else if (request_start > request_end) {
  1787. if (acthd >= request_start || acthd < request_end)
  1788. return true;
  1789. }
  1790. return false;
  1791. }
  1792. static bool i915_request_guilty(struct drm_i915_gem_request *request,
  1793. const u32 acthd, bool *inside)
  1794. {
  1795. /* There is a possibility that unmasked head address
  1796. * pointing inside the ring, matches the batch_obj address range.
  1797. * However this is extremely unlikely.
  1798. */
  1799. if (request->batch_obj) {
  1800. if (i915_head_inside_object(acthd, request->batch_obj)) {
  1801. *inside = true;
  1802. return true;
  1803. }
  1804. }
  1805. if (i915_head_inside_request(acthd, request->head, request->tail)) {
  1806. *inside = false;
  1807. return true;
  1808. }
  1809. return false;
  1810. }
  1811. static void i915_set_reset_status(struct intel_ring_buffer *ring,
  1812. struct drm_i915_gem_request *request,
  1813. u32 acthd)
  1814. {
  1815. struct i915_ctx_hang_stats *hs = NULL;
  1816. bool inside, guilty;
  1817. /* Innocent until proven guilty */
  1818. guilty = false;
  1819. if (ring->hangcheck.action != wait &&
  1820. i915_request_guilty(request, acthd, &inside)) {
  1821. DRM_ERROR("%s hung %s bo (0x%x ctx %d) at 0x%x\n",
  1822. ring->name,
  1823. inside ? "inside" : "flushing",
  1824. request->batch_obj ?
  1825. request->batch_obj->gtt_offset : 0,
  1826. request->ctx ? request->ctx->id : 0,
  1827. acthd);
  1828. guilty = true;
  1829. }
  1830. /* If contexts are disabled or this is the default context, use
  1831. * file_priv->reset_state
  1832. */
  1833. if (request->ctx && request->ctx->id != DEFAULT_CONTEXT_ID)
  1834. hs = &request->ctx->hang_stats;
  1835. else if (request->file_priv)
  1836. hs = &request->file_priv->hang_stats;
  1837. if (hs) {
  1838. if (guilty)
  1839. hs->batch_active++;
  1840. else
  1841. hs->batch_pending++;
  1842. }
  1843. }
  1844. static void i915_gem_free_request(struct drm_i915_gem_request *request)
  1845. {
  1846. list_del(&request->list);
  1847. i915_gem_request_remove_from_client(request);
  1848. if (request->ctx)
  1849. i915_gem_context_unreference(request->ctx);
  1850. kfree(request);
  1851. }
  1852. static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
  1853. struct intel_ring_buffer *ring)
  1854. {
  1855. u32 completed_seqno;
  1856. u32 acthd;
  1857. acthd = intel_ring_get_active_head(ring);
  1858. completed_seqno = ring->get_seqno(ring, false);
  1859. while (!list_empty(&ring->request_list)) {
  1860. struct drm_i915_gem_request *request;
  1861. request = list_first_entry(&ring->request_list,
  1862. struct drm_i915_gem_request,
  1863. list);
  1864. if (request->seqno > completed_seqno)
  1865. i915_set_reset_status(ring, request, acthd);
  1866. i915_gem_free_request(request);
  1867. }
  1868. while (!list_empty(&ring->active_list)) {
  1869. struct drm_i915_gem_object *obj;
  1870. obj = list_first_entry(&ring->active_list,
  1871. struct drm_i915_gem_object,
  1872. ring_list);
  1873. i915_gem_object_move_to_inactive(obj);
  1874. }
  1875. }
  1876. static void i915_gem_reset_fences(struct drm_device *dev)
  1877. {
  1878. struct drm_i915_private *dev_priv = dev->dev_private;
  1879. int i;
  1880. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  1881. struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  1882. if (reg->obj)
  1883. i915_gem_object_fence_lost(reg->obj);
  1884. i915_gem_write_fence(dev, i, NULL);
  1885. reg->pin_count = 0;
  1886. reg->obj = NULL;
  1887. INIT_LIST_HEAD(&reg->lru_list);
  1888. }
  1889. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  1890. }
  1891. void i915_gem_reset(struct drm_device *dev)
  1892. {
  1893. struct drm_i915_private *dev_priv = dev->dev_private;
  1894. struct drm_i915_gem_object *obj;
  1895. struct intel_ring_buffer *ring;
  1896. int i;
  1897. for_each_ring(ring, dev_priv, i)
  1898. i915_gem_reset_ring_lists(dev_priv, ring);
  1899. /* Move everything out of the GPU domains to ensure we do any
  1900. * necessary invalidation upon reuse.
  1901. */
  1902. list_for_each_entry(obj,
  1903. &dev_priv->mm.inactive_list,
  1904. mm_list)
  1905. {
  1906. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  1907. }
  1908. /* The fence registers are invalidated so clear them out */
  1909. i915_gem_reset_fences(dev);
  1910. }
  1911. /**
  1912. * This function clears the request list as sequence numbers are passed.
  1913. */
  1914. void
  1915. i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
  1916. {
  1917. uint32_t seqno;
  1918. if (list_empty(&ring->request_list))
  1919. return;
  1920. WARN_ON(i915_verify_lists(ring->dev));
  1921. seqno = ring->get_seqno(ring, true);
  1922. while (!list_empty(&ring->request_list)) {
  1923. struct drm_i915_gem_request *request;
  1924. request = list_first_entry(&ring->request_list,
  1925. struct drm_i915_gem_request,
  1926. list);
  1927. if (!i915_seqno_passed(seqno, request->seqno))
  1928. break;
  1929. trace_i915_gem_request_retire(ring, request->seqno);
  1930. /* We know the GPU must have read the request to have
  1931. * sent us the seqno + interrupt, so use the position
  1932. * of tail of the request to update the last known position
  1933. * of the GPU head.
  1934. */
  1935. ring->last_retired_head = request->tail;
  1936. i915_gem_free_request(request);
  1937. }
  1938. /* Move any buffers on the active list that are no longer referenced
  1939. * by the ringbuffer to the flushing/inactive lists as appropriate.
  1940. */
  1941. while (!list_empty(&ring->active_list)) {
  1942. struct drm_i915_gem_object *obj;
  1943. obj = list_first_entry(&ring->active_list,
  1944. struct drm_i915_gem_object,
  1945. ring_list);
  1946. if (!i915_seqno_passed(seqno, obj->last_read_seqno))
  1947. break;
  1948. i915_gem_object_move_to_inactive(obj);
  1949. }
  1950. if (unlikely(ring->trace_irq_seqno &&
  1951. i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
  1952. ring->irq_put(ring);
  1953. ring->trace_irq_seqno = 0;
  1954. }
  1955. WARN_ON(i915_verify_lists(ring->dev));
  1956. }
  1957. void
  1958. i915_gem_retire_requests(struct drm_device *dev)
  1959. {
  1960. drm_i915_private_t *dev_priv = dev->dev_private;
  1961. struct intel_ring_buffer *ring;
  1962. int i;
  1963. for_each_ring(ring, dev_priv, i)
  1964. i915_gem_retire_requests_ring(ring);
  1965. }
  1966. static void
  1967. i915_gem_retire_work_handler(struct work_struct *work)
  1968. {
  1969. drm_i915_private_t *dev_priv;
  1970. struct drm_device *dev;
  1971. struct intel_ring_buffer *ring;
  1972. bool idle;
  1973. int i;
  1974. dev_priv = container_of(work, drm_i915_private_t,
  1975. mm.retire_work.work);
  1976. dev = dev_priv->dev;
  1977. /* Come back later if the device is busy... */
  1978. if (!mutex_trylock(&dev->struct_mutex)) {
  1979. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  1980. round_jiffies_up_relative(HZ));
  1981. return;
  1982. }
  1983. i915_gem_retire_requests(dev);
  1984. /* Send a periodic flush down the ring so we don't hold onto GEM
  1985. * objects indefinitely.
  1986. */
  1987. idle = true;
  1988. for_each_ring(ring, dev_priv, i) {
  1989. if (ring->gpu_caches_dirty)
  1990. i915_add_request(ring, NULL);
  1991. idle &= list_empty(&ring->request_list);
  1992. }
  1993. if (!dev_priv->mm.suspended && !idle)
  1994. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  1995. round_jiffies_up_relative(HZ));
  1996. if (idle)
  1997. intel_mark_idle(dev);
  1998. mutex_unlock(&dev->struct_mutex);
  1999. }
  2000. /**
  2001. * Ensures that an object will eventually get non-busy by flushing any required
  2002. * write domains, emitting any outstanding lazy request and retiring and
  2003. * completed requests.
  2004. */
  2005. static int
  2006. i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
  2007. {
  2008. int ret;
  2009. if (obj->active) {
  2010. ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno);
  2011. if (ret)
  2012. return ret;
  2013. i915_gem_retire_requests_ring(obj->ring);
  2014. }
  2015. return 0;
  2016. }
  2017. /**
  2018. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2019. * @DRM_IOCTL_ARGS: standard ioctl arguments
  2020. *
  2021. * Returns 0 if successful, else an error is returned with the remaining time in
  2022. * the timeout parameter.
  2023. * -ETIME: object is still busy after timeout
  2024. * -ERESTARTSYS: signal interrupted the wait
  2025. * -ENONENT: object doesn't exist
  2026. * Also possible, but rare:
  2027. * -EAGAIN: GPU wedged
  2028. * -ENOMEM: damn
  2029. * -ENODEV: Internal IRQ fail
  2030. * -E?: The add request failed
  2031. *
  2032. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2033. * non-zero timeout parameter the wait ioctl will wait for the given number of
  2034. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2035. * without holding struct_mutex the object may become re-busied before this
  2036. * function completes. A similar but shorter * race condition exists in the busy
  2037. * ioctl
  2038. */
  2039. int
  2040. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2041. {
  2042. drm_i915_private_t *dev_priv = dev->dev_private;
  2043. struct drm_i915_gem_wait *args = data;
  2044. struct drm_i915_gem_object *obj;
  2045. struct intel_ring_buffer *ring = NULL;
  2046. struct timespec timeout_stack, *timeout = NULL;
  2047. unsigned reset_counter;
  2048. u32 seqno = 0;
  2049. int ret = 0;
  2050. if (args->timeout_ns >= 0) {
  2051. timeout_stack = ns_to_timespec(args->timeout_ns);
  2052. timeout = &timeout_stack;
  2053. }
  2054. ret = i915_mutex_lock_interruptible(dev);
  2055. if (ret)
  2056. return ret;
  2057. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
  2058. if (&obj->base == NULL) {
  2059. mutex_unlock(&dev->struct_mutex);
  2060. return -ENOENT;
  2061. }
  2062. /* Need to make sure the object gets inactive eventually. */
  2063. ret = i915_gem_object_flush_active(obj);
  2064. if (ret)
  2065. goto out;
  2066. if (obj->active) {
  2067. seqno = obj->last_read_seqno;
  2068. ring = obj->ring;
  2069. }
  2070. if (seqno == 0)
  2071. goto out;
  2072. /* Do this after OLR check to make sure we make forward progress polling
  2073. * on this IOCTL with a 0 timeout (like busy ioctl)
  2074. */
  2075. if (!args->timeout_ns) {
  2076. ret = -ETIME;
  2077. goto out;
  2078. }
  2079. drm_gem_object_unreference(&obj->base);
  2080. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  2081. mutex_unlock(&dev->struct_mutex);
  2082. ret = __wait_seqno(ring, seqno, reset_counter, true, timeout);
  2083. if (timeout)
  2084. args->timeout_ns = timespec_to_ns(timeout);
  2085. return ret;
  2086. out:
  2087. drm_gem_object_unreference(&obj->base);
  2088. mutex_unlock(&dev->struct_mutex);
  2089. return ret;
  2090. }
  2091. /**
  2092. * i915_gem_object_sync - sync an object to a ring.
  2093. *
  2094. * @obj: object which may be in use on another ring.
  2095. * @to: ring we wish to use the object on. May be NULL.
  2096. *
  2097. * This code is meant to abstract object synchronization with the GPU.
  2098. * Calling with NULL implies synchronizing the object with the CPU
  2099. * rather than a particular GPU ring.
  2100. *
  2101. * Returns 0 if successful, else propagates up the lower layer error.
  2102. */
  2103. int
  2104. i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2105. struct intel_ring_buffer *to)
  2106. {
  2107. struct intel_ring_buffer *from = obj->ring;
  2108. u32 seqno;
  2109. int ret, idx;
  2110. if (from == NULL || to == from)
  2111. return 0;
  2112. if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
  2113. return i915_gem_object_wait_rendering(obj, false);
  2114. idx = intel_ring_sync_index(from, to);
  2115. seqno = obj->last_read_seqno;
  2116. if (seqno <= from->sync_seqno[idx])
  2117. return 0;
  2118. ret = i915_gem_check_olr(obj->ring, seqno);
  2119. if (ret)
  2120. return ret;
  2121. ret = to->sync_to(to, from, seqno);
  2122. if (!ret)
  2123. /* We use last_read_seqno because sync_to()
  2124. * might have just caused seqno wrap under
  2125. * the radar.
  2126. */
  2127. from->sync_seqno[idx] = obj->last_read_seqno;
  2128. return ret;
  2129. }
  2130. static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
  2131. {
  2132. u32 old_write_domain, old_read_domains;
  2133. /* Force a pagefault for domain tracking on next user access */
  2134. i915_gem_release_mmap(obj);
  2135. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2136. return;
  2137. /* Wait for any direct GTT access to complete */
  2138. mb();
  2139. old_read_domains = obj->base.read_domains;
  2140. old_write_domain = obj->base.write_domain;
  2141. obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
  2142. obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
  2143. trace_i915_gem_object_change_domain(obj,
  2144. old_read_domains,
  2145. old_write_domain);
  2146. }
  2147. /**
  2148. * Unbinds an object from the GTT aperture.
  2149. */
  2150. int
  2151. i915_gem_object_unbind(struct drm_i915_gem_object *obj)
  2152. {
  2153. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  2154. int ret;
  2155. if (obj->gtt_space == NULL)
  2156. return 0;
  2157. if (obj->pin_count)
  2158. return -EBUSY;
  2159. BUG_ON(obj->pages == NULL);
  2160. ret = i915_gem_object_finish_gpu(obj);
  2161. if (ret)
  2162. return ret;
  2163. /* Continue on if we fail due to EIO, the GPU is hung so we
  2164. * should be safe and we need to cleanup or else we might
  2165. * cause memory corruption through use-after-free.
  2166. */
  2167. i915_gem_object_finish_gtt(obj);
  2168. /* release the fence reg _after_ flushing */
  2169. ret = i915_gem_object_put_fence(obj);
  2170. if (ret)
  2171. return ret;
  2172. trace_i915_gem_object_unbind(obj);
  2173. if (obj->has_global_gtt_mapping)
  2174. i915_gem_gtt_unbind_object(obj);
  2175. if (obj->has_aliasing_ppgtt_mapping) {
  2176. i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
  2177. obj->has_aliasing_ppgtt_mapping = 0;
  2178. }
  2179. i915_gem_gtt_finish_object(obj);
  2180. i915_gem_object_unpin_pages(obj);
  2181. list_del(&obj->mm_list);
  2182. list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  2183. /* Avoid an unnecessary call to unbind on rebind. */
  2184. obj->map_and_fenceable = true;
  2185. drm_mm_put_block(obj->gtt_space);
  2186. obj->gtt_space = NULL;
  2187. obj->gtt_offset = 0;
  2188. return 0;
  2189. }
  2190. int i915_gpu_idle(struct drm_device *dev)
  2191. {
  2192. drm_i915_private_t *dev_priv = dev->dev_private;
  2193. struct intel_ring_buffer *ring;
  2194. int ret, i;
  2195. /* Flush everything onto the inactive list. */
  2196. for_each_ring(ring, dev_priv, i) {
  2197. ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID);
  2198. if (ret)
  2199. return ret;
  2200. ret = intel_ring_idle(ring);
  2201. if (ret)
  2202. return ret;
  2203. }
  2204. return 0;
  2205. }
  2206. static void i965_write_fence_reg(struct drm_device *dev, int reg,
  2207. struct drm_i915_gem_object *obj)
  2208. {
  2209. drm_i915_private_t *dev_priv = dev->dev_private;
  2210. int fence_reg;
  2211. int fence_pitch_shift;
  2212. uint64_t val;
  2213. if (INTEL_INFO(dev)->gen >= 6) {
  2214. fence_reg = FENCE_REG_SANDYBRIDGE_0;
  2215. fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT;
  2216. } else {
  2217. fence_reg = FENCE_REG_965_0;
  2218. fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
  2219. }
  2220. if (obj) {
  2221. u32 size = obj->gtt_space->size;
  2222. val = (uint64_t)((obj->gtt_offset + size - 4096) &
  2223. 0xfffff000) << 32;
  2224. val |= obj->gtt_offset & 0xfffff000;
  2225. val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift;
  2226. if (obj->tiling_mode == I915_TILING_Y)
  2227. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  2228. val |= I965_FENCE_REG_VALID;
  2229. } else
  2230. val = 0;
  2231. fence_reg += reg * 8;
  2232. I915_WRITE64(fence_reg, val);
  2233. POSTING_READ(fence_reg);
  2234. }
  2235. static void i915_write_fence_reg(struct drm_device *dev, int reg,
  2236. struct drm_i915_gem_object *obj)
  2237. {
  2238. drm_i915_private_t *dev_priv = dev->dev_private;
  2239. u32 val;
  2240. if (obj) {
  2241. u32 size = obj->gtt_space->size;
  2242. int pitch_val;
  2243. int tile_width;
  2244. WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
  2245. (size & -size) != size ||
  2246. (obj->gtt_offset & (size - 1)),
  2247. "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
  2248. obj->gtt_offset, obj->map_and_fenceable, size);
  2249. if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
  2250. tile_width = 128;
  2251. else
  2252. tile_width = 512;
  2253. /* Note: pitch better be a power of two tile widths */
  2254. pitch_val = obj->stride / tile_width;
  2255. pitch_val = ffs(pitch_val) - 1;
  2256. val = obj->gtt_offset;
  2257. if (obj->tiling_mode == I915_TILING_Y)
  2258. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2259. val |= I915_FENCE_SIZE_BITS(size);
  2260. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2261. val |= I830_FENCE_REG_VALID;
  2262. } else
  2263. val = 0;
  2264. if (reg < 8)
  2265. reg = FENCE_REG_830_0 + reg * 4;
  2266. else
  2267. reg = FENCE_REG_945_8 + (reg - 8) * 4;
  2268. I915_WRITE(reg, val);
  2269. POSTING_READ(reg);
  2270. }
  2271. static void i830_write_fence_reg(struct drm_device *dev, int reg,
  2272. struct drm_i915_gem_object *obj)
  2273. {
  2274. drm_i915_private_t *dev_priv = dev->dev_private;
  2275. uint32_t val;
  2276. if (obj) {
  2277. u32 size = obj->gtt_space->size;
  2278. uint32_t pitch_val;
  2279. WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
  2280. (size & -size) != size ||
  2281. (obj->gtt_offset & (size - 1)),
  2282. "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
  2283. obj->gtt_offset, size);
  2284. pitch_val = obj->stride / 128;
  2285. pitch_val = ffs(pitch_val) - 1;
  2286. val = obj->gtt_offset;
  2287. if (obj->tiling_mode == I915_TILING_Y)
  2288. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2289. val |= I830_FENCE_SIZE_BITS(size);
  2290. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2291. val |= I830_FENCE_REG_VALID;
  2292. } else
  2293. val = 0;
  2294. I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
  2295. POSTING_READ(FENCE_REG_830_0 + reg * 4);
  2296. }
  2297. inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj)
  2298. {
  2299. return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT;
  2300. }
  2301. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  2302. struct drm_i915_gem_object *obj)
  2303. {
  2304. struct drm_i915_private *dev_priv = dev->dev_private;
  2305. /* Ensure that all CPU reads are completed before installing a fence
  2306. * and all writes before removing the fence.
  2307. */
  2308. if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj))
  2309. mb();
  2310. switch (INTEL_INFO(dev)->gen) {
  2311. case 7:
  2312. case 6:
  2313. case 5:
  2314. case 4: i965_write_fence_reg(dev, reg, obj); break;
  2315. case 3: i915_write_fence_reg(dev, reg, obj); break;
  2316. case 2: i830_write_fence_reg(dev, reg, obj); break;
  2317. default: BUG();
  2318. }
  2319. /* And similarly be paranoid that no direct access to this region
  2320. * is reordered to before the fence is installed.
  2321. */
  2322. if (i915_gem_object_needs_mb(obj))
  2323. mb();
  2324. }
  2325. static inline int fence_number(struct drm_i915_private *dev_priv,
  2326. struct drm_i915_fence_reg *fence)
  2327. {
  2328. return fence - dev_priv->fence_regs;
  2329. }
  2330. struct write_fence {
  2331. struct drm_device *dev;
  2332. struct drm_i915_gem_object *obj;
  2333. int fence;
  2334. };
  2335. static void i915_gem_write_fence__ipi(void *data)
  2336. {
  2337. struct write_fence *args = data;
  2338. /* Required for SNB+ with LLC */
  2339. wbinvd();
  2340. /* Required for VLV */
  2341. i915_gem_write_fence(args->dev, args->fence, args->obj);
  2342. }
  2343. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  2344. struct drm_i915_fence_reg *fence,
  2345. bool enable)
  2346. {
  2347. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2348. struct write_fence args = {
  2349. .dev = obj->base.dev,
  2350. .fence = fence_number(dev_priv, fence),
  2351. .obj = enable ? obj : NULL,
  2352. };
  2353. /* In order to fully serialize access to the fenced region and
  2354. * the update to the fence register we need to take extreme
  2355. * measures on SNB+. In theory, the write to the fence register
  2356. * flushes all memory transactions before, and coupled with the
  2357. * mb() placed around the register write we serialise all memory
  2358. * operations with respect to the changes in the tiler. Yet, on
  2359. * SNB+ we need to take a step further and emit an explicit wbinvd()
  2360. * on each processor in order to manually flush all memory
  2361. * transactions before updating the fence register.
  2362. *
  2363. * However, Valleyview complicates matter. There the wbinvd is
  2364. * insufficient and unlike SNB/IVB requires the serialising
  2365. * register write. (Note that that register write by itself is
  2366. * conversely not sufficient for SNB+.) To compromise, we do both.
  2367. */
  2368. if (INTEL_INFO(args.dev)->gen >= 6)
  2369. on_each_cpu(i915_gem_write_fence__ipi, &args, 1);
  2370. else
  2371. i915_gem_write_fence(args.dev, args.fence, args.obj);
  2372. if (enable) {
  2373. obj->fence_reg = args.fence;
  2374. fence->obj = obj;
  2375. list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
  2376. } else {
  2377. obj->fence_reg = I915_FENCE_REG_NONE;
  2378. fence->obj = NULL;
  2379. list_del_init(&fence->lru_list);
  2380. }
  2381. }
  2382. static int
  2383. i915_gem_object_wait_fence(struct drm_i915_gem_object *obj)
  2384. {
  2385. if (obj->last_fenced_seqno) {
  2386. int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno);
  2387. if (ret)
  2388. return ret;
  2389. obj->last_fenced_seqno = 0;
  2390. }
  2391. obj->fenced_gpu_access = false;
  2392. return 0;
  2393. }
  2394. int
  2395. i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
  2396. {
  2397. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2398. struct drm_i915_fence_reg *fence;
  2399. int ret;
  2400. ret = i915_gem_object_wait_fence(obj);
  2401. if (ret)
  2402. return ret;
  2403. if (obj->fence_reg == I915_FENCE_REG_NONE)
  2404. return 0;
  2405. fence = &dev_priv->fence_regs[obj->fence_reg];
  2406. i915_gem_object_fence_lost(obj);
  2407. i915_gem_object_update_fence(obj, fence, false);
  2408. return 0;
  2409. }
  2410. static struct drm_i915_fence_reg *
  2411. i915_find_fence_reg(struct drm_device *dev)
  2412. {
  2413. struct drm_i915_private *dev_priv = dev->dev_private;
  2414. struct drm_i915_fence_reg *reg, *avail;
  2415. int i;
  2416. /* First try to find a free reg */
  2417. avail = NULL;
  2418. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  2419. reg = &dev_priv->fence_regs[i];
  2420. if (!reg->obj)
  2421. return reg;
  2422. if (!reg->pin_count)
  2423. avail = reg;
  2424. }
  2425. if (avail == NULL)
  2426. return NULL;
  2427. /* None available, try to steal one or wait for a user to finish */
  2428. list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
  2429. if (reg->pin_count)
  2430. continue;
  2431. return reg;
  2432. }
  2433. return NULL;
  2434. }
  2435. /**
  2436. * i915_gem_object_get_fence - set up fencing for an object
  2437. * @obj: object to map through a fence reg
  2438. *
  2439. * When mapping objects through the GTT, userspace wants to be able to write
  2440. * to them without having to worry about swizzling if the object is tiled.
  2441. * This function walks the fence regs looking for a free one for @obj,
  2442. * stealing one if it can't find any.
  2443. *
  2444. * It then sets up the reg based on the object's properties: address, pitch
  2445. * and tiling format.
  2446. *
  2447. * For an untiled surface, this removes any existing fence.
  2448. */
  2449. int
  2450. i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
  2451. {
  2452. struct drm_device *dev = obj->base.dev;
  2453. struct drm_i915_private *dev_priv = dev->dev_private;
  2454. bool enable = obj->tiling_mode != I915_TILING_NONE;
  2455. struct drm_i915_fence_reg *reg;
  2456. int ret;
  2457. /* Have we updated the tiling parameters upon the object and so
  2458. * will need to serialise the write to the associated fence register?
  2459. */
  2460. if (obj->fence_dirty) {
  2461. ret = i915_gem_object_wait_fence(obj);
  2462. if (ret)
  2463. return ret;
  2464. }
  2465. /* Just update our place in the LRU if our fence is getting reused. */
  2466. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  2467. reg = &dev_priv->fence_regs[obj->fence_reg];
  2468. if (!obj->fence_dirty) {
  2469. list_move_tail(&reg->lru_list,
  2470. &dev_priv->mm.fence_list);
  2471. return 0;
  2472. }
  2473. } else if (enable) {
  2474. reg = i915_find_fence_reg(dev);
  2475. if (reg == NULL)
  2476. return -EDEADLK;
  2477. if (reg->obj) {
  2478. struct drm_i915_gem_object *old = reg->obj;
  2479. ret = i915_gem_object_wait_fence(old);
  2480. if (ret)
  2481. return ret;
  2482. i915_gem_object_fence_lost(old);
  2483. }
  2484. } else
  2485. return 0;
  2486. i915_gem_object_update_fence(obj, reg, enable);
  2487. obj->fence_dirty = false;
  2488. return 0;
  2489. }
  2490. static bool i915_gem_valid_gtt_space(struct drm_device *dev,
  2491. struct drm_mm_node *gtt_space,
  2492. unsigned long cache_level)
  2493. {
  2494. struct drm_mm_node *other;
  2495. /* On non-LLC machines we have to be careful when putting differing
  2496. * types of snoopable memory together to avoid the prefetcher
  2497. * crossing memory domains and dying.
  2498. */
  2499. if (HAS_LLC(dev))
  2500. return true;
  2501. if (gtt_space == NULL)
  2502. return true;
  2503. if (list_empty(&gtt_space->node_list))
  2504. return true;
  2505. other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
  2506. if (other->allocated && !other->hole_follows && other->color != cache_level)
  2507. return false;
  2508. other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
  2509. if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
  2510. return false;
  2511. return true;
  2512. }
  2513. static void i915_gem_verify_gtt(struct drm_device *dev)
  2514. {
  2515. #if WATCH_GTT
  2516. struct drm_i915_private *dev_priv = dev->dev_private;
  2517. struct drm_i915_gem_object *obj;
  2518. int err = 0;
  2519. list_for_each_entry(obj, &dev_priv->mm.gtt_list, global_list) {
  2520. if (obj->gtt_space == NULL) {
  2521. printk(KERN_ERR "object found on GTT list with no space reserved\n");
  2522. err++;
  2523. continue;
  2524. }
  2525. if (obj->cache_level != obj->gtt_space->color) {
  2526. printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n",
  2527. obj->gtt_space->start,
  2528. obj->gtt_space->start + obj->gtt_space->size,
  2529. obj->cache_level,
  2530. obj->gtt_space->color);
  2531. err++;
  2532. continue;
  2533. }
  2534. if (!i915_gem_valid_gtt_space(dev,
  2535. obj->gtt_space,
  2536. obj->cache_level)) {
  2537. printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n",
  2538. obj->gtt_space->start,
  2539. obj->gtt_space->start + obj->gtt_space->size,
  2540. obj->cache_level);
  2541. err++;
  2542. continue;
  2543. }
  2544. }
  2545. WARN_ON(err);
  2546. #endif
  2547. }
  2548. /**
  2549. * Finds free space in the GTT aperture and binds the object there.
  2550. */
  2551. static int
  2552. i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
  2553. unsigned alignment,
  2554. bool map_and_fenceable,
  2555. bool nonblocking)
  2556. {
  2557. struct drm_device *dev = obj->base.dev;
  2558. drm_i915_private_t *dev_priv = dev->dev_private;
  2559. struct drm_mm_node *node;
  2560. u32 size, fence_size, fence_alignment, unfenced_alignment;
  2561. bool mappable, fenceable;
  2562. size_t gtt_max = map_and_fenceable ?
  2563. dev_priv->gtt.mappable_end : dev_priv->gtt.total;
  2564. int ret;
  2565. fence_size = i915_gem_get_gtt_size(dev,
  2566. obj->base.size,
  2567. obj->tiling_mode);
  2568. fence_alignment = i915_gem_get_gtt_alignment(dev,
  2569. obj->base.size,
  2570. obj->tiling_mode, true);
  2571. unfenced_alignment =
  2572. i915_gem_get_gtt_alignment(dev,
  2573. obj->base.size,
  2574. obj->tiling_mode, false);
  2575. if (alignment == 0)
  2576. alignment = map_and_fenceable ? fence_alignment :
  2577. unfenced_alignment;
  2578. if (map_and_fenceable && alignment & (fence_alignment - 1)) {
  2579. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  2580. return -EINVAL;
  2581. }
  2582. size = map_and_fenceable ? fence_size : obj->base.size;
  2583. /* If the object is bigger than the entire aperture, reject it early
  2584. * before evicting everything in a vain attempt to find space.
  2585. */
  2586. if (obj->base.size > gtt_max) {
  2587. DRM_ERROR("Attempting to bind an object larger than the aperture: object=%zd > %s aperture=%zu\n",
  2588. obj->base.size,
  2589. map_and_fenceable ? "mappable" : "total",
  2590. gtt_max);
  2591. return -E2BIG;
  2592. }
  2593. ret = i915_gem_object_get_pages(obj);
  2594. if (ret)
  2595. return ret;
  2596. i915_gem_object_pin_pages(obj);
  2597. node = kzalloc(sizeof(*node), GFP_KERNEL);
  2598. if (node == NULL) {
  2599. i915_gem_object_unpin_pages(obj);
  2600. return -ENOMEM;
  2601. }
  2602. search_free:
  2603. ret = drm_mm_insert_node_in_range_generic(&dev_priv->mm.gtt_space, node,
  2604. size, alignment,
  2605. obj->cache_level, 0, gtt_max);
  2606. if (ret) {
  2607. ret = i915_gem_evict_something(dev, size, alignment,
  2608. obj->cache_level,
  2609. map_and_fenceable,
  2610. nonblocking);
  2611. if (ret == 0)
  2612. goto search_free;
  2613. i915_gem_object_unpin_pages(obj);
  2614. kfree(node);
  2615. return ret;
  2616. }
  2617. if (WARN_ON(!i915_gem_valid_gtt_space(dev, node, obj->cache_level))) {
  2618. i915_gem_object_unpin_pages(obj);
  2619. drm_mm_put_block(node);
  2620. return -EINVAL;
  2621. }
  2622. ret = i915_gem_gtt_prepare_object(obj);
  2623. if (ret) {
  2624. i915_gem_object_unpin_pages(obj);
  2625. drm_mm_put_block(node);
  2626. return ret;
  2627. }
  2628. list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
  2629. list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  2630. obj->gtt_space = node;
  2631. obj->gtt_offset = node->start;
  2632. fenceable =
  2633. node->size == fence_size &&
  2634. (node->start & (fence_alignment - 1)) == 0;
  2635. mappable =
  2636. obj->gtt_offset + obj->base.size <= dev_priv->gtt.mappable_end;
  2637. obj->map_and_fenceable = mappable && fenceable;
  2638. trace_i915_gem_object_bind(obj, map_and_fenceable);
  2639. i915_gem_verify_gtt(dev);
  2640. return 0;
  2641. }
  2642. void
  2643. i915_gem_clflush_object(struct drm_i915_gem_object *obj)
  2644. {
  2645. /* If we don't have a page list set up, then we're not pinned
  2646. * to GPU, and we can ignore the cache flush because it'll happen
  2647. * again at bind time.
  2648. */
  2649. if (obj->pages == NULL)
  2650. return;
  2651. /*
  2652. * Stolen memory is always coherent with the GPU as it is explicitly
  2653. * marked as wc by the system, or the system is cache-coherent.
  2654. */
  2655. if (obj->stolen)
  2656. return;
  2657. /* If the GPU is snooping the contents of the CPU cache,
  2658. * we do not need to manually clear the CPU cache lines. However,
  2659. * the caches are only snooped when the render cache is
  2660. * flushed/invalidated. As we always have to emit invalidations
  2661. * and flushes when moving into and out of the RENDER domain, correct
  2662. * snooping behaviour occurs naturally as the result of our domain
  2663. * tracking.
  2664. */
  2665. if (obj->cache_level != I915_CACHE_NONE)
  2666. return;
  2667. trace_i915_gem_object_clflush(obj);
  2668. drm_clflush_sg(obj->pages);
  2669. }
  2670. /** Flushes the GTT write domain for the object if it's dirty. */
  2671. static void
  2672. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  2673. {
  2674. uint32_t old_write_domain;
  2675. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  2676. return;
  2677. /* No actual flushing is required for the GTT write domain. Writes
  2678. * to it immediately go to main memory as far as we know, so there's
  2679. * no chipset flush. It also doesn't land in render cache.
  2680. *
  2681. * However, we do have to enforce the order so that all writes through
  2682. * the GTT land before any writes to the device, such as updates to
  2683. * the GATT itself.
  2684. */
  2685. wmb();
  2686. old_write_domain = obj->base.write_domain;
  2687. obj->base.write_domain = 0;
  2688. trace_i915_gem_object_change_domain(obj,
  2689. obj->base.read_domains,
  2690. old_write_domain);
  2691. }
  2692. /** Flushes the CPU write domain for the object if it's dirty. */
  2693. static void
  2694. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
  2695. {
  2696. uint32_t old_write_domain;
  2697. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  2698. return;
  2699. i915_gem_clflush_object(obj);
  2700. i915_gem_chipset_flush(obj->base.dev);
  2701. old_write_domain = obj->base.write_domain;
  2702. obj->base.write_domain = 0;
  2703. trace_i915_gem_object_change_domain(obj,
  2704. obj->base.read_domains,
  2705. old_write_domain);
  2706. }
  2707. /**
  2708. * Moves a single object to the GTT read, and possibly write domain.
  2709. *
  2710. * This function returns when the move is complete, including waiting on
  2711. * flushes to occur.
  2712. */
  2713. int
  2714. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  2715. {
  2716. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  2717. uint32_t old_write_domain, old_read_domains;
  2718. int ret;
  2719. /* Not valid to be called on unbound objects. */
  2720. if (obj->gtt_space == NULL)
  2721. return -EINVAL;
  2722. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  2723. return 0;
  2724. ret = i915_gem_object_wait_rendering(obj, !write);
  2725. if (ret)
  2726. return ret;
  2727. i915_gem_object_flush_cpu_write_domain(obj);
  2728. /* Serialise direct access to this object with the barriers for
  2729. * coherent writes from the GPU, by effectively invalidating the
  2730. * GTT domain upon first access.
  2731. */
  2732. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2733. mb();
  2734. old_write_domain = obj->base.write_domain;
  2735. old_read_domains = obj->base.read_domains;
  2736. /* It should now be out of any other write domains, and we can update
  2737. * the domain values for our changes.
  2738. */
  2739. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2740. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2741. if (write) {
  2742. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  2743. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  2744. obj->dirty = 1;
  2745. }
  2746. trace_i915_gem_object_change_domain(obj,
  2747. old_read_domains,
  2748. old_write_domain);
  2749. /* And bump the LRU for this access */
  2750. if (i915_gem_object_is_inactive(obj))
  2751. list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  2752. return 0;
  2753. }
  2754. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  2755. enum i915_cache_level cache_level)
  2756. {
  2757. struct drm_device *dev = obj->base.dev;
  2758. drm_i915_private_t *dev_priv = dev->dev_private;
  2759. int ret;
  2760. if (obj->cache_level == cache_level)
  2761. return 0;
  2762. if (obj->pin_count) {
  2763. DRM_DEBUG("can not change the cache level of pinned objects\n");
  2764. return -EBUSY;
  2765. }
  2766. if (!i915_gem_valid_gtt_space(dev, obj->gtt_space, cache_level)) {
  2767. ret = i915_gem_object_unbind(obj);
  2768. if (ret)
  2769. return ret;
  2770. }
  2771. if (obj->gtt_space) {
  2772. ret = i915_gem_object_finish_gpu(obj);
  2773. if (ret)
  2774. return ret;
  2775. i915_gem_object_finish_gtt(obj);
  2776. /* Before SandyBridge, you could not use tiling or fence
  2777. * registers with snooped memory, so relinquish any fences
  2778. * currently pointing to our region in the aperture.
  2779. */
  2780. if (INTEL_INFO(dev)->gen < 6) {
  2781. ret = i915_gem_object_put_fence(obj);
  2782. if (ret)
  2783. return ret;
  2784. }
  2785. if (obj->has_global_gtt_mapping)
  2786. i915_gem_gtt_bind_object(obj, cache_level);
  2787. if (obj->has_aliasing_ppgtt_mapping)
  2788. i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
  2789. obj, cache_level);
  2790. obj->gtt_space->color = cache_level;
  2791. }
  2792. if (cache_level == I915_CACHE_NONE) {
  2793. u32 old_read_domains, old_write_domain;
  2794. /* If we're coming from LLC cached, then we haven't
  2795. * actually been tracking whether the data is in the
  2796. * CPU cache or not, since we only allow one bit set
  2797. * in obj->write_domain and have been skipping the clflushes.
  2798. * Just set it to the CPU cache for now.
  2799. */
  2800. WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
  2801. WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
  2802. old_read_domains = obj->base.read_domains;
  2803. old_write_domain = obj->base.write_domain;
  2804. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2805. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2806. trace_i915_gem_object_change_domain(obj,
  2807. old_read_domains,
  2808. old_write_domain);
  2809. }
  2810. obj->cache_level = cache_level;
  2811. i915_gem_verify_gtt(dev);
  2812. return 0;
  2813. }
  2814. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  2815. struct drm_file *file)
  2816. {
  2817. struct drm_i915_gem_caching *args = data;
  2818. struct drm_i915_gem_object *obj;
  2819. int ret;
  2820. ret = i915_mutex_lock_interruptible(dev);
  2821. if (ret)
  2822. return ret;
  2823. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2824. if (&obj->base == NULL) {
  2825. ret = -ENOENT;
  2826. goto unlock;
  2827. }
  2828. args->caching = obj->cache_level != I915_CACHE_NONE;
  2829. drm_gem_object_unreference(&obj->base);
  2830. unlock:
  2831. mutex_unlock(&dev->struct_mutex);
  2832. return ret;
  2833. }
  2834. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  2835. struct drm_file *file)
  2836. {
  2837. struct drm_i915_gem_caching *args = data;
  2838. struct drm_i915_gem_object *obj;
  2839. enum i915_cache_level level;
  2840. int ret;
  2841. switch (args->caching) {
  2842. case I915_CACHING_NONE:
  2843. level = I915_CACHE_NONE;
  2844. break;
  2845. case I915_CACHING_CACHED:
  2846. level = I915_CACHE_LLC;
  2847. break;
  2848. default:
  2849. return -EINVAL;
  2850. }
  2851. ret = i915_mutex_lock_interruptible(dev);
  2852. if (ret)
  2853. return ret;
  2854. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2855. if (&obj->base == NULL) {
  2856. ret = -ENOENT;
  2857. goto unlock;
  2858. }
  2859. ret = i915_gem_object_set_cache_level(obj, level);
  2860. drm_gem_object_unreference(&obj->base);
  2861. unlock:
  2862. mutex_unlock(&dev->struct_mutex);
  2863. return ret;
  2864. }
  2865. /*
  2866. * Prepare buffer for display plane (scanout, cursors, etc).
  2867. * Can be called from an uninterruptible phase (modesetting) and allows
  2868. * any flushes to be pipelined (for pageflips).
  2869. */
  2870. int
  2871. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  2872. u32 alignment,
  2873. struct intel_ring_buffer *pipelined)
  2874. {
  2875. u32 old_read_domains, old_write_domain;
  2876. int ret;
  2877. if (pipelined != obj->ring) {
  2878. ret = i915_gem_object_sync(obj, pipelined);
  2879. if (ret)
  2880. return ret;
  2881. }
  2882. /* The display engine is not coherent with the LLC cache on gen6. As
  2883. * a result, we make sure that the pinning that is about to occur is
  2884. * done with uncached PTEs. This is lowest common denominator for all
  2885. * chipsets.
  2886. *
  2887. * However for gen6+, we could do better by using the GFDT bit instead
  2888. * of uncaching, which would allow us to flush all the LLC-cached data
  2889. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  2890. */
  2891. ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
  2892. if (ret)
  2893. return ret;
  2894. /* As the user may map the buffer once pinned in the display plane
  2895. * (e.g. libkms for the bootup splash), we have to ensure that we
  2896. * always use map_and_fenceable for all scanout buffers.
  2897. */
  2898. ret = i915_gem_object_pin(obj, alignment, true, false);
  2899. if (ret)
  2900. return ret;
  2901. i915_gem_object_flush_cpu_write_domain(obj);
  2902. old_write_domain = obj->base.write_domain;
  2903. old_read_domains = obj->base.read_domains;
  2904. /* It should now be out of any other write domains, and we can update
  2905. * the domain values for our changes.
  2906. */
  2907. obj->base.write_domain = 0;
  2908. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2909. trace_i915_gem_object_change_domain(obj,
  2910. old_read_domains,
  2911. old_write_domain);
  2912. return 0;
  2913. }
  2914. int
  2915. i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
  2916. {
  2917. int ret;
  2918. if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
  2919. return 0;
  2920. ret = i915_gem_object_wait_rendering(obj, false);
  2921. if (ret)
  2922. return ret;
  2923. /* Ensure that we invalidate the GPU's caches and TLBs. */
  2924. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  2925. return 0;
  2926. }
  2927. /**
  2928. * Moves a single object to the CPU read, and possibly write domain.
  2929. *
  2930. * This function returns when the move is complete, including waiting on
  2931. * flushes to occur.
  2932. */
  2933. int
  2934. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  2935. {
  2936. uint32_t old_write_domain, old_read_domains;
  2937. int ret;
  2938. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  2939. return 0;
  2940. ret = i915_gem_object_wait_rendering(obj, !write);
  2941. if (ret)
  2942. return ret;
  2943. i915_gem_object_flush_gtt_write_domain(obj);
  2944. old_write_domain = obj->base.write_domain;
  2945. old_read_domains = obj->base.read_domains;
  2946. /* Flush the CPU cache if it's still invalid. */
  2947. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  2948. i915_gem_clflush_object(obj);
  2949. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  2950. }
  2951. /* It should now be out of any other write domains, and we can update
  2952. * the domain values for our changes.
  2953. */
  2954. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2955. /* If we're writing through the CPU, then the GPU read domains will
  2956. * need to be invalidated at next use.
  2957. */
  2958. if (write) {
  2959. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2960. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2961. }
  2962. trace_i915_gem_object_change_domain(obj,
  2963. old_read_domains,
  2964. old_write_domain);
  2965. return 0;
  2966. }
  2967. /* Throttle our rendering by waiting until the ring has completed our requests
  2968. * emitted over 20 msec ago.
  2969. *
  2970. * Note that if we were to use the current jiffies each time around the loop,
  2971. * we wouldn't escape the function with any frames outstanding if the time to
  2972. * render a frame was over 20ms.
  2973. *
  2974. * This should get us reasonable parallelism between CPU and GPU but also
  2975. * relatively low latency when blocking on a particular request to finish.
  2976. */
  2977. static int
  2978. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  2979. {
  2980. struct drm_i915_private *dev_priv = dev->dev_private;
  2981. struct drm_i915_file_private *file_priv = file->driver_priv;
  2982. unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
  2983. struct drm_i915_gem_request *request;
  2984. struct intel_ring_buffer *ring = NULL;
  2985. unsigned reset_counter;
  2986. u32 seqno = 0;
  2987. int ret;
  2988. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  2989. if (ret)
  2990. return ret;
  2991. ret = i915_gem_check_wedge(&dev_priv->gpu_error, false);
  2992. if (ret)
  2993. return ret;
  2994. spin_lock(&file_priv->mm.lock);
  2995. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  2996. if (time_after_eq(request->emitted_jiffies, recent_enough))
  2997. break;
  2998. ring = request->ring;
  2999. seqno = request->seqno;
  3000. }
  3001. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  3002. spin_unlock(&file_priv->mm.lock);
  3003. if (seqno == 0)
  3004. return 0;
  3005. ret = __wait_seqno(ring, seqno, reset_counter, true, NULL);
  3006. if (ret == 0)
  3007. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
  3008. return ret;
  3009. }
  3010. int
  3011. i915_gem_object_pin(struct drm_i915_gem_object *obj,
  3012. uint32_t alignment,
  3013. bool map_and_fenceable,
  3014. bool nonblocking)
  3015. {
  3016. int ret;
  3017. if (WARN_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
  3018. return -EBUSY;
  3019. if (obj->gtt_space != NULL) {
  3020. if ((alignment && obj->gtt_offset & (alignment - 1)) ||
  3021. (map_and_fenceable && !obj->map_and_fenceable)) {
  3022. WARN(obj->pin_count,
  3023. "bo is already pinned with incorrect alignment:"
  3024. " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
  3025. " obj->map_and_fenceable=%d\n",
  3026. obj->gtt_offset, alignment,
  3027. map_and_fenceable,
  3028. obj->map_and_fenceable);
  3029. ret = i915_gem_object_unbind(obj);
  3030. if (ret)
  3031. return ret;
  3032. }
  3033. }
  3034. if (obj->gtt_space == NULL) {
  3035. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3036. ret = i915_gem_object_bind_to_gtt(obj, alignment,
  3037. map_and_fenceable,
  3038. nonblocking);
  3039. if (ret)
  3040. return ret;
  3041. if (!dev_priv->mm.aliasing_ppgtt)
  3042. i915_gem_gtt_bind_object(obj, obj->cache_level);
  3043. }
  3044. if (!obj->has_global_gtt_mapping && map_and_fenceable)
  3045. i915_gem_gtt_bind_object(obj, obj->cache_level);
  3046. obj->pin_count++;
  3047. obj->pin_mappable |= map_and_fenceable;
  3048. return 0;
  3049. }
  3050. void
  3051. i915_gem_object_unpin(struct drm_i915_gem_object *obj)
  3052. {
  3053. BUG_ON(obj->pin_count == 0);
  3054. BUG_ON(obj->gtt_space == NULL);
  3055. if (--obj->pin_count == 0)
  3056. obj->pin_mappable = false;
  3057. }
  3058. int
  3059. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  3060. struct drm_file *file)
  3061. {
  3062. struct drm_i915_gem_pin *args = data;
  3063. struct drm_i915_gem_object *obj;
  3064. int ret;
  3065. ret = i915_mutex_lock_interruptible(dev);
  3066. if (ret)
  3067. return ret;
  3068. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3069. if (&obj->base == NULL) {
  3070. ret = -ENOENT;
  3071. goto unlock;
  3072. }
  3073. if (obj->madv != I915_MADV_WILLNEED) {
  3074. DRM_ERROR("Attempting to pin a purgeable buffer\n");
  3075. ret = -EINVAL;
  3076. goto out;
  3077. }
  3078. if (obj->pin_filp != NULL && obj->pin_filp != file) {
  3079. DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
  3080. args->handle);
  3081. ret = -EINVAL;
  3082. goto out;
  3083. }
  3084. if (obj->user_pin_count == 0) {
  3085. ret = i915_gem_object_pin(obj, args->alignment, true, false);
  3086. if (ret)
  3087. goto out;
  3088. }
  3089. obj->user_pin_count++;
  3090. obj->pin_filp = file;
  3091. /* XXX - flush the CPU caches for pinned objects
  3092. * as the X server doesn't manage domains yet
  3093. */
  3094. i915_gem_object_flush_cpu_write_domain(obj);
  3095. args->offset = obj->gtt_offset;
  3096. out:
  3097. drm_gem_object_unreference(&obj->base);
  3098. unlock:
  3099. mutex_unlock(&dev->struct_mutex);
  3100. return ret;
  3101. }
  3102. int
  3103. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  3104. struct drm_file *file)
  3105. {
  3106. struct drm_i915_gem_pin *args = data;
  3107. struct drm_i915_gem_object *obj;
  3108. int ret;
  3109. ret = i915_mutex_lock_interruptible(dev);
  3110. if (ret)
  3111. return ret;
  3112. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3113. if (&obj->base == NULL) {
  3114. ret = -ENOENT;
  3115. goto unlock;
  3116. }
  3117. if (obj->pin_filp != file) {
  3118. DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  3119. args->handle);
  3120. ret = -EINVAL;
  3121. goto out;
  3122. }
  3123. obj->user_pin_count--;
  3124. if (obj->user_pin_count == 0) {
  3125. obj->pin_filp = NULL;
  3126. i915_gem_object_unpin(obj);
  3127. }
  3128. out:
  3129. drm_gem_object_unreference(&obj->base);
  3130. unlock:
  3131. mutex_unlock(&dev->struct_mutex);
  3132. return ret;
  3133. }
  3134. int
  3135. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3136. struct drm_file *file)
  3137. {
  3138. struct drm_i915_gem_busy *args = data;
  3139. struct drm_i915_gem_object *obj;
  3140. int ret;
  3141. ret = i915_mutex_lock_interruptible(dev);
  3142. if (ret)
  3143. return ret;
  3144. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3145. if (&obj->base == NULL) {
  3146. ret = -ENOENT;
  3147. goto unlock;
  3148. }
  3149. /* Count all active objects as busy, even if they are currently not used
  3150. * by the gpu. Users of this interface expect objects to eventually
  3151. * become non-busy without any further actions, therefore emit any
  3152. * necessary flushes here.
  3153. */
  3154. ret = i915_gem_object_flush_active(obj);
  3155. args->busy = obj->active;
  3156. if (obj->ring) {
  3157. BUILD_BUG_ON(I915_NUM_RINGS > 16);
  3158. args->busy |= intel_ring_flag(obj->ring) << 16;
  3159. }
  3160. drm_gem_object_unreference(&obj->base);
  3161. unlock:
  3162. mutex_unlock(&dev->struct_mutex);
  3163. return ret;
  3164. }
  3165. int
  3166. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3167. struct drm_file *file_priv)
  3168. {
  3169. return i915_gem_ring_throttle(dev, file_priv);
  3170. }
  3171. int
  3172. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3173. struct drm_file *file_priv)
  3174. {
  3175. struct drm_i915_gem_madvise *args = data;
  3176. struct drm_i915_gem_object *obj;
  3177. int ret;
  3178. switch (args->madv) {
  3179. case I915_MADV_DONTNEED:
  3180. case I915_MADV_WILLNEED:
  3181. break;
  3182. default:
  3183. return -EINVAL;
  3184. }
  3185. ret = i915_mutex_lock_interruptible(dev);
  3186. if (ret)
  3187. return ret;
  3188. obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
  3189. if (&obj->base == NULL) {
  3190. ret = -ENOENT;
  3191. goto unlock;
  3192. }
  3193. if (obj->pin_count) {
  3194. ret = -EINVAL;
  3195. goto out;
  3196. }
  3197. if (obj->madv != __I915_MADV_PURGED)
  3198. obj->madv = args->madv;
  3199. /* if the object is no longer attached, discard its backing storage */
  3200. if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL)
  3201. i915_gem_object_truncate(obj);
  3202. args->retained = obj->madv != __I915_MADV_PURGED;
  3203. out:
  3204. drm_gem_object_unreference(&obj->base);
  3205. unlock:
  3206. mutex_unlock(&dev->struct_mutex);
  3207. return ret;
  3208. }
  3209. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3210. const struct drm_i915_gem_object_ops *ops)
  3211. {
  3212. INIT_LIST_HEAD(&obj->mm_list);
  3213. INIT_LIST_HEAD(&obj->global_list);
  3214. INIT_LIST_HEAD(&obj->ring_list);
  3215. INIT_LIST_HEAD(&obj->exec_list);
  3216. obj->ops = ops;
  3217. obj->fence_reg = I915_FENCE_REG_NONE;
  3218. obj->madv = I915_MADV_WILLNEED;
  3219. /* Avoid an unnecessary call to unbind on the first bind. */
  3220. obj->map_and_fenceable = true;
  3221. i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
  3222. }
  3223. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3224. .get_pages = i915_gem_object_get_pages_gtt,
  3225. .put_pages = i915_gem_object_put_pages_gtt,
  3226. };
  3227. struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
  3228. size_t size)
  3229. {
  3230. struct drm_i915_gem_object *obj;
  3231. struct address_space *mapping;
  3232. gfp_t mask;
  3233. obj = i915_gem_object_alloc(dev);
  3234. if (obj == NULL)
  3235. return NULL;
  3236. if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  3237. i915_gem_object_free(obj);
  3238. return NULL;
  3239. }
  3240. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3241. if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
  3242. /* 965gm cannot relocate objects above 4GiB. */
  3243. mask &= ~__GFP_HIGHMEM;
  3244. mask |= __GFP_DMA32;
  3245. }
  3246. mapping = file_inode(obj->base.filp)->i_mapping;
  3247. mapping_set_gfp_mask(mapping, mask);
  3248. i915_gem_object_init(obj, &i915_gem_object_ops);
  3249. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3250. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3251. if (HAS_LLC(dev)) {
  3252. /* On some devices, we can have the GPU use the LLC (the CPU
  3253. * cache) for about a 10% performance improvement
  3254. * compared to uncached. Graphics requests other than
  3255. * display scanout are coherent with the CPU in
  3256. * accessing this cache. This means in this mode we
  3257. * don't need to clflush on the CPU side, and on the
  3258. * GPU side we only need to flush internal caches to
  3259. * get data visible to the CPU.
  3260. *
  3261. * However, we maintain the display planes as UC, and so
  3262. * need to rebind when first used as such.
  3263. */
  3264. obj->cache_level = I915_CACHE_LLC;
  3265. } else
  3266. obj->cache_level = I915_CACHE_NONE;
  3267. return obj;
  3268. }
  3269. int i915_gem_init_object(struct drm_gem_object *obj)
  3270. {
  3271. BUG();
  3272. return 0;
  3273. }
  3274. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3275. {
  3276. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3277. struct drm_device *dev = obj->base.dev;
  3278. drm_i915_private_t *dev_priv = dev->dev_private;
  3279. trace_i915_gem_object_destroy(obj);
  3280. if (obj->phys_obj)
  3281. i915_gem_detach_phys_object(dev, obj);
  3282. obj->pin_count = 0;
  3283. if (WARN_ON(i915_gem_object_unbind(obj) == -ERESTARTSYS)) {
  3284. bool was_interruptible;
  3285. was_interruptible = dev_priv->mm.interruptible;
  3286. dev_priv->mm.interruptible = false;
  3287. WARN_ON(i915_gem_object_unbind(obj));
  3288. dev_priv->mm.interruptible = was_interruptible;
  3289. }
  3290. /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
  3291. * before progressing. */
  3292. if (obj->stolen)
  3293. i915_gem_object_unpin_pages(obj);
  3294. if (WARN_ON(obj->pages_pin_count))
  3295. obj->pages_pin_count = 0;
  3296. i915_gem_object_put_pages(obj);
  3297. i915_gem_object_free_mmap_offset(obj);
  3298. i915_gem_object_release_stolen(obj);
  3299. BUG_ON(obj->pages);
  3300. if (obj->base.import_attach)
  3301. drm_prime_gem_destroy(&obj->base, NULL);
  3302. drm_gem_object_release(&obj->base);
  3303. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  3304. kfree(obj->bit_17);
  3305. i915_gem_object_free(obj);
  3306. }
  3307. int
  3308. i915_gem_idle(struct drm_device *dev)
  3309. {
  3310. drm_i915_private_t *dev_priv = dev->dev_private;
  3311. int ret;
  3312. mutex_lock(&dev->struct_mutex);
  3313. if (dev_priv->mm.suspended) {
  3314. mutex_unlock(&dev->struct_mutex);
  3315. return 0;
  3316. }
  3317. ret = i915_gpu_idle(dev);
  3318. if (ret) {
  3319. mutex_unlock(&dev->struct_mutex);
  3320. return ret;
  3321. }
  3322. i915_gem_retire_requests(dev);
  3323. /* Under UMS, be paranoid and evict. */
  3324. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3325. i915_gem_evict_everything(dev);
  3326. i915_gem_reset_fences(dev);
  3327. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  3328. * We need to replace this with a semaphore, or something.
  3329. * And not confound mm.suspended!
  3330. */
  3331. dev_priv->mm.suspended = 1;
  3332. del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
  3333. i915_kernel_lost_context(dev);
  3334. i915_gem_cleanup_ringbuffer(dev);
  3335. mutex_unlock(&dev->struct_mutex);
  3336. /* Cancel the retire work handler, which should be idle now. */
  3337. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  3338. return 0;
  3339. }
  3340. void i915_gem_l3_remap(struct drm_device *dev)
  3341. {
  3342. drm_i915_private_t *dev_priv = dev->dev_private;
  3343. u32 misccpctl;
  3344. int i;
  3345. if (!HAS_L3_GPU_CACHE(dev))
  3346. return;
  3347. if (!dev_priv->l3_parity.remap_info)
  3348. return;
  3349. misccpctl = I915_READ(GEN7_MISCCPCTL);
  3350. I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
  3351. POSTING_READ(GEN7_MISCCPCTL);
  3352. for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
  3353. u32 remap = I915_READ(GEN7_L3LOG_BASE + i);
  3354. if (remap && remap != dev_priv->l3_parity.remap_info[i/4])
  3355. DRM_DEBUG("0x%x was already programmed to %x\n",
  3356. GEN7_L3LOG_BASE + i, remap);
  3357. if (remap && !dev_priv->l3_parity.remap_info[i/4])
  3358. DRM_DEBUG_DRIVER("Clearing remapped register\n");
  3359. I915_WRITE(GEN7_L3LOG_BASE + i, dev_priv->l3_parity.remap_info[i/4]);
  3360. }
  3361. /* Make sure all the writes land before disabling dop clock gating */
  3362. POSTING_READ(GEN7_L3LOG_BASE);
  3363. I915_WRITE(GEN7_MISCCPCTL, misccpctl);
  3364. }
  3365. void i915_gem_init_swizzling(struct drm_device *dev)
  3366. {
  3367. drm_i915_private_t *dev_priv = dev->dev_private;
  3368. if (INTEL_INFO(dev)->gen < 5 ||
  3369. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  3370. return;
  3371. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  3372. DISP_TILE_SURFACE_SWIZZLING);
  3373. if (IS_GEN5(dev))
  3374. return;
  3375. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  3376. if (IS_GEN6(dev))
  3377. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  3378. else if (IS_GEN7(dev))
  3379. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  3380. else
  3381. BUG();
  3382. }
  3383. static bool
  3384. intel_enable_blt(struct drm_device *dev)
  3385. {
  3386. if (!HAS_BLT(dev))
  3387. return false;
  3388. /* The blitter was dysfunctional on early prototypes */
  3389. if (IS_GEN6(dev) && dev->pdev->revision < 8) {
  3390. DRM_INFO("BLT not supported on this pre-production hardware;"
  3391. " graphics performance will be degraded.\n");
  3392. return false;
  3393. }
  3394. return true;
  3395. }
  3396. static int i915_gem_init_rings(struct drm_device *dev)
  3397. {
  3398. struct drm_i915_private *dev_priv = dev->dev_private;
  3399. int ret;
  3400. ret = intel_init_render_ring_buffer(dev);
  3401. if (ret)
  3402. return ret;
  3403. if (HAS_BSD(dev)) {
  3404. ret = intel_init_bsd_ring_buffer(dev);
  3405. if (ret)
  3406. goto cleanup_render_ring;
  3407. }
  3408. if (intel_enable_blt(dev)) {
  3409. ret = intel_init_blt_ring_buffer(dev);
  3410. if (ret)
  3411. goto cleanup_bsd_ring;
  3412. }
  3413. if (HAS_VEBOX(dev)) {
  3414. ret = intel_init_vebox_ring_buffer(dev);
  3415. if (ret)
  3416. goto cleanup_blt_ring;
  3417. }
  3418. ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
  3419. if (ret)
  3420. goto cleanup_vebox_ring;
  3421. return 0;
  3422. cleanup_vebox_ring:
  3423. intel_cleanup_ring_buffer(&dev_priv->ring[VECS]);
  3424. cleanup_blt_ring:
  3425. intel_cleanup_ring_buffer(&dev_priv->ring[BCS]);
  3426. cleanup_bsd_ring:
  3427. intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
  3428. cleanup_render_ring:
  3429. intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
  3430. return ret;
  3431. }
  3432. int
  3433. i915_gem_init_hw(struct drm_device *dev)
  3434. {
  3435. drm_i915_private_t *dev_priv = dev->dev_private;
  3436. int ret;
  3437. if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
  3438. return -EIO;
  3439. if (IS_HASWELL(dev) && (I915_READ(0x120010) == 1))
  3440. I915_WRITE(0x9008, I915_READ(0x9008) | 0xf0000);
  3441. if (HAS_PCH_NOP(dev)) {
  3442. u32 temp = I915_READ(GEN7_MSG_CTL);
  3443. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  3444. I915_WRITE(GEN7_MSG_CTL, temp);
  3445. }
  3446. i915_gem_l3_remap(dev);
  3447. i915_gem_init_swizzling(dev);
  3448. ret = i915_gem_init_rings(dev);
  3449. if (ret)
  3450. return ret;
  3451. /*
  3452. * XXX: There was some w/a described somewhere suggesting loading
  3453. * contexts before PPGTT.
  3454. */
  3455. i915_gem_context_init(dev);
  3456. if (dev_priv->mm.aliasing_ppgtt) {
  3457. ret = dev_priv->mm.aliasing_ppgtt->enable(dev);
  3458. if (ret) {
  3459. i915_gem_cleanup_aliasing_ppgtt(dev);
  3460. DRM_INFO("PPGTT enable failed. This is not fatal, but unexpected\n");
  3461. }
  3462. }
  3463. return 0;
  3464. }
  3465. int i915_gem_init(struct drm_device *dev)
  3466. {
  3467. struct drm_i915_private *dev_priv = dev->dev_private;
  3468. int ret;
  3469. mutex_lock(&dev->struct_mutex);
  3470. if (IS_VALLEYVIEW(dev)) {
  3471. /* VLVA0 (potential hack), BIOS isn't actually waking us */
  3472. I915_WRITE(VLV_GTLC_WAKE_CTRL, 1);
  3473. if (wait_for((I915_READ(VLV_GTLC_PW_STATUS) & 1) == 1, 10))
  3474. DRM_DEBUG_DRIVER("allow wake ack timed out\n");
  3475. }
  3476. i915_gem_init_global_gtt(dev);
  3477. ret = i915_gem_init_hw(dev);
  3478. mutex_unlock(&dev->struct_mutex);
  3479. if (ret) {
  3480. i915_gem_cleanup_aliasing_ppgtt(dev);
  3481. return ret;
  3482. }
  3483. /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */
  3484. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3485. dev_priv->dri1.allow_batchbuffer = 1;
  3486. return 0;
  3487. }
  3488. void
  3489. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  3490. {
  3491. drm_i915_private_t *dev_priv = dev->dev_private;
  3492. struct intel_ring_buffer *ring;
  3493. int i;
  3494. for_each_ring(ring, dev_priv, i)
  3495. intel_cleanup_ring_buffer(ring);
  3496. }
  3497. int
  3498. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  3499. struct drm_file *file_priv)
  3500. {
  3501. drm_i915_private_t *dev_priv = dev->dev_private;
  3502. int ret;
  3503. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3504. return 0;
  3505. if (i915_reset_in_progress(&dev_priv->gpu_error)) {
  3506. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  3507. atomic_set(&dev_priv->gpu_error.reset_counter, 0);
  3508. }
  3509. mutex_lock(&dev->struct_mutex);
  3510. dev_priv->mm.suspended = 0;
  3511. ret = i915_gem_init_hw(dev);
  3512. if (ret != 0) {
  3513. mutex_unlock(&dev->struct_mutex);
  3514. return ret;
  3515. }
  3516. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  3517. mutex_unlock(&dev->struct_mutex);
  3518. ret = drm_irq_install(dev);
  3519. if (ret)
  3520. goto cleanup_ringbuffer;
  3521. return 0;
  3522. cleanup_ringbuffer:
  3523. mutex_lock(&dev->struct_mutex);
  3524. i915_gem_cleanup_ringbuffer(dev);
  3525. dev_priv->mm.suspended = 1;
  3526. mutex_unlock(&dev->struct_mutex);
  3527. return ret;
  3528. }
  3529. int
  3530. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  3531. struct drm_file *file_priv)
  3532. {
  3533. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3534. return 0;
  3535. drm_irq_uninstall(dev);
  3536. return i915_gem_idle(dev);
  3537. }
  3538. void
  3539. i915_gem_lastclose(struct drm_device *dev)
  3540. {
  3541. int ret;
  3542. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3543. return;
  3544. ret = i915_gem_idle(dev);
  3545. if (ret)
  3546. DRM_ERROR("failed to idle hardware: %d\n", ret);
  3547. }
  3548. static void
  3549. init_ring_lists(struct intel_ring_buffer *ring)
  3550. {
  3551. INIT_LIST_HEAD(&ring->active_list);
  3552. INIT_LIST_HEAD(&ring->request_list);
  3553. }
  3554. void
  3555. i915_gem_load(struct drm_device *dev)
  3556. {
  3557. drm_i915_private_t *dev_priv = dev->dev_private;
  3558. int i;
  3559. dev_priv->slab =
  3560. kmem_cache_create("i915_gem_object",
  3561. sizeof(struct drm_i915_gem_object), 0,
  3562. SLAB_HWCACHE_ALIGN,
  3563. NULL);
  3564. INIT_LIST_HEAD(&dev_priv->mm.active_list);
  3565. INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
  3566. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  3567. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  3568. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  3569. for (i = 0; i < I915_NUM_RINGS; i++)
  3570. init_ring_lists(&dev_priv->ring[i]);
  3571. for (i = 0; i < I915_MAX_NUM_FENCES; i++)
  3572. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  3573. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  3574. i915_gem_retire_work_handler);
  3575. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  3576. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  3577. if (IS_GEN3(dev)) {
  3578. I915_WRITE(MI_ARB_STATE,
  3579. _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
  3580. }
  3581. dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  3582. /* Old X drivers will take 0-2 for front, back, depth buffers */
  3583. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3584. dev_priv->fence_reg_start = 3;
  3585. if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev))
  3586. dev_priv->num_fence_regs = 32;
  3587. else if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3588. dev_priv->num_fence_regs = 16;
  3589. else
  3590. dev_priv->num_fence_regs = 8;
  3591. /* Initialize fence registers to zero */
  3592. i915_gem_reset_fences(dev);
  3593. i915_gem_detect_bit_6_swizzle(dev);
  3594. init_waitqueue_head(&dev_priv->pending_flip_queue);
  3595. dev_priv->mm.interruptible = true;
  3596. dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
  3597. dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
  3598. register_shrinker(&dev_priv->mm.inactive_shrinker);
  3599. }
  3600. /*
  3601. * Create a physically contiguous memory object for this object
  3602. * e.g. for cursor + overlay regs
  3603. */
  3604. static int i915_gem_init_phys_object(struct drm_device *dev,
  3605. int id, int size, int align)
  3606. {
  3607. drm_i915_private_t *dev_priv = dev->dev_private;
  3608. struct drm_i915_gem_phys_object *phys_obj;
  3609. int ret;
  3610. if (dev_priv->mm.phys_objs[id - 1] || !size)
  3611. return 0;
  3612. phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
  3613. if (!phys_obj)
  3614. return -ENOMEM;
  3615. phys_obj->id = id;
  3616. phys_obj->handle = drm_pci_alloc(dev, size, align);
  3617. if (!phys_obj->handle) {
  3618. ret = -ENOMEM;
  3619. goto kfree_obj;
  3620. }
  3621. #ifdef CONFIG_X86
  3622. set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3623. #endif
  3624. dev_priv->mm.phys_objs[id - 1] = phys_obj;
  3625. return 0;
  3626. kfree_obj:
  3627. kfree(phys_obj);
  3628. return ret;
  3629. }
  3630. static void i915_gem_free_phys_object(struct drm_device *dev, int id)
  3631. {
  3632. drm_i915_private_t *dev_priv = dev->dev_private;
  3633. struct drm_i915_gem_phys_object *phys_obj;
  3634. if (!dev_priv->mm.phys_objs[id - 1])
  3635. return;
  3636. phys_obj = dev_priv->mm.phys_objs[id - 1];
  3637. if (phys_obj->cur_obj) {
  3638. i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
  3639. }
  3640. #ifdef CONFIG_X86
  3641. set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3642. #endif
  3643. drm_pci_free(dev, phys_obj->handle);
  3644. kfree(phys_obj);
  3645. dev_priv->mm.phys_objs[id - 1] = NULL;
  3646. }
  3647. void i915_gem_free_all_phys_object(struct drm_device *dev)
  3648. {
  3649. int i;
  3650. for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
  3651. i915_gem_free_phys_object(dev, i);
  3652. }
  3653. void i915_gem_detach_phys_object(struct drm_device *dev,
  3654. struct drm_i915_gem_object *obj)
  3655. {
  3656. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  3657. char *vaddr;
  3658. int i;
  3659. int page_count;
  3660. if (!obj->phys_obj)
  3661. return;
  3662. vaddr = obj->phys_obj->handle->vaddr;
  3663. page_count = obj->base.size / PAGE_SIZE;
  3664. for (i = 0; i < page_count; i++) {
  3665. struct page *page = shmem_read_mapping_page(mapping, i);
  3666. if (!IS_ERR(page)) {
  3667. char *dst = kmap_atomic(page);
  3668. memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
  3669. kunmap_atomic(dst);
  3670. drm_clflush_pages(&page, 1);
  3671. set_page_dirty(page);
  3672. mark_page_accessed(page);
  3673. page_cache_release(page);
  3674. }
  3675. }
  3676. i915_gem_chipset_flush(dev);
  3677. obj->phys_obj->cur_obj = NULL;
  3678. obj->phys_obj = NULL;
  3679. }
  3680. int
  3681. i915_gem_attach_phys_object(struct drm_device *dev,
  3682. struct drm_i915_gem_object *obj,
  3683. int id,
  3684. int align)
  3685. {
  3686. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  3687. drm_i915_private_t *dev_priv = dev->dev_private;
  3688. int ret = 0;
  3689. int page_count;
  3690. int i;
  3691. if (id > I915_MAX_PHYS_OBJECT)
  3692. return -EINVAL;
  3693. if (obj->phys_obj) {
  3694. if (obj->phys_obj->id == id)
  3695. return 0;
  3696. i915_gem_detach_phys_object(dev, obj);
  3697. }
  3698. /* create a new object */
  3699. if (!dev_priv->mm.phys_objs[id - 1]) {
  3700. ret = i915_gem_init_phys_object(dev, id,
  3701. obj->base.size, align);
  3702. if (ret) {
  3703. DRM_ERROR("failed to init phys object %d size: %zu\n",
  3704. id, obj->base.size);
  3705. return ret;
  3706. }
  3707. }
  3708. /* bind to the object */
  3709. obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
  3710. obj->phys_obj->cur_obj = obj;
  3711. page_count = obj->base.size / PAGE_SIZE;
  3712. for (i = 0; i < page_count; i++) {
  3713. struct page *page;
  3714. char *dst, *src;
  3715. page = shmem_read_mapping_page(mapping, i);
  3716. if (IS_ERR(page))
  3717. return PTR_ERR(page);
  3718. src = kmap_atomic(page);
  3719. dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  3720. memcpy(dst, src, PAGE_SIZE);
  3721. kunmap_atomic(src);
  3722. mark_page_accessed(page);
  3723. page_cache_release(page);
  3724. }
  3725. return 0;
  3726. }
  3727. static int
  3728. i915_gem_phys_pwrite(struct drm_device *dev,
  3729. struct drm_i915_gem_object *obj,
  3730. struct drm_i915_gem_pwrite *args,
  3731. struct drm_file *file_priv)
  3732. {
  3733. void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
  3734. char __user *user_data = to_user_ptr(args->data_ptr);
  3735. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  3736. unsigned long unwritten;
  3737. /* The physical object once assigned is fixed for the lifetime
  3738. * of the obj, so we can safely drop the lock and continue
  3739. * to access vaddr.
  3740. */
  3741. mutex_unlock(&dev->struct_mutex);
  3742. unwritten = copy_from_user(vaddr, user_data, args->size);
  3743. mutex_lock(&dev->struct_mutex);
  3744. if (unwritten)
  3745. return -EFAULT;
  3746. }
  3747. i915_gem_chipset_flush(dev);
  3748. return 0;
  3749. }
  3750. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  3751. {
  3752. struct drm_i915_file_private *file_priv = file->driver_priv;
  3753. /* Clean up our request list when the client is going away, so that
  3754. * later retire_requests won't dereference our soon-to-be-gone
  3755. * file_priv.
  3756. */
  3757. spin_lock(&file_priv->mm.lock);
  3758. while (!list_empty(&file_priv->mm.request_list)) {
  3759. struct drm_i915_gem_request *request;
  3760. request = list_first_entry(&file_priv->mm.request_list,
  3761. struct drm_i915_gem_request,
  3762. client_list);
  3763. list_del(&request->client_list);
  3764. request->file_priv = NULL;
  3765. }
  3766. spin_unlock(&file_priv->mm.lock);
  3767. }
  3768. static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task)
  3769. {
  3770. if (!mutex_is_locked(mutex))
  3771. return false;
  3772. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)
  3773. return mutex->owner == task;
  3774. #else
  3775. /* Since UP may be pre-empted, we cannot assume that we own the lock */
  3776. return false;
  3777. #endif
  3778. }
  3779. static int
  3780. i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
  3781. {
  3782. struct drm_i915_private *dev_priv =
  3783. container_of(shrinker,
  3784. struct drm_i915_private,
  3785. mm.inactive_shrinker);
  3786. struct drm_device *dev = dev_priv->dev;
  3787. struct drm_i915_gem_object *obj;
  3788. int nr_to_scan = sc->nr_to_scan;
  3789. bool unlock = true;
  3790. int cnt;
  3791. if (!mutex_trylock(&dev->struct_mutex)) {
  3792. if (!mutex_is_locked_by(&dev->struct_mutex, current))
  3793. return 0;
  3794. if (dev_priv->mm.shrinker_no_lock_stealing)
  3795. return 0;
  3796. unlock = false;
  3797. }
  3798. if (nr_to_scan) {
  3799. nr_to_scan -= i915_gem_purge(dev_priv, nr_to_scan);
  3800. if (nr_to_scan > 0)
  3801. nr_to_scan -= __i915_gem_shrink(dev_priv, nr_to_scan,
  3802. false);
  3803. if (nr_to_scan > 0)
  3804. i915_gem_shrink_all(dev_priv);
  3805. }
  3806. cnt = 0;
  3807. list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list)
  3808. if (obj->pages_pin_count == 0)
  3809. cnt += obj->base.size >> PAGE_SHIFT;
  3810. list_for_each_entry(obj, &dev_priv->mm.inactive_list, global_list)
  3811. if (obj->pin_count == 0 && obj->pages_pin_count == 0)
  3812. cnt += obj->base.size >> PAGE_SHIFT;
  3813. if (unlock)
  3814. mutex_unlock(&dev->struct_mutex);
  3815. return cnt;
  3816. }